CSharp - Use GroupBy operator with an elementSelector method and a comparer object

Introduction

We want to group the dates of awarded options by whether they were awarded to a founding Student, where a founding Student is one whose id is less than 100.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program// www  . ja  v  a  2  s . c  o m
{
    static void Main(string[] args)
    {
        MyFounderNumberComparer comp = new MyFounderNumberComparer();
        StudentOptionEntry[] empOptions = StudentOptionEntry.GetStudentOptionEntries();
        IEnumerable<IGrouping<int, DateTime>> opts = empOptions
          .GroupBy(o => o.id, o => o.dateAwarded, comp);

        //  First enumerate through the sequence of IGroupings.
        foreach (IGrouping<int, DateTime> keyGroup in opts)
        {
            Console.WriteLine("Option records for: " +
              (comp.isFounder(keyGroup.Key) ? "founder" : "non-founder"));

            //  Now enumerate through the grouping's sequence of StudentOptionEntry elements.
            foreach (DateTime date in keyGroup)
                Console.WriteLine(date.ToShortDateString());
        }
    }
}
public class MyFounderNumberComparer : IEqualityComparer<int>
{
    public bool Equals(int x, int y)
    {
        return (isFounder(x) == isFounder(y));
    }

    public int GetHashCode(int i)
    {
        int f = 1;
        int nf = 100;
        return (isFounder(i) ? f.GetHashCode() : nf.GetHashCode());
    }

    public bool isFounder(int id)
    {
        return (id < 100);
    }
}
class StudentOptionEntry
{
    public int id;
    public long optionsCount;
    public DateTime dateAwarded;

    public static StudentOptionEntry[] GetStudentOptionEntries()
    {
        StudentOptionEntry[] empOptions = new StudentOptionEntry[] {
      new StudentOptionEntry {
        id = 1,
        optionsCount = 2,
        dateAwarded = DateTime.Parse("1990/12/31") },
      new StudentOptionEntry {
        id = 2,
        optionsCount = 3000,
        dateAwarded = DateTime.Parse("1992/06/30")  },
      new StudentOptionEntry {
        id = 2,
        optionsCount = 3000,
        dateAwarded = DateTime.Parse("1991/01/01")  },
      new StudentOptionEntry {
        id = 3,
        optionsCount = 5000,
        dateAwarded = DateTime.Parse("1997/09/30") },
      new StudentOptionEntry {
        id = 2,
        optionsCount = 3000,
        dateAwarded = DateTime.Parse("2000/04/01")  },
      new StudentOptionEntry {
        id = 3,
        optionsCount = 7500,
        dateAwarded = DateTime.Parse("1998/09/30") },
      new StudentOptionEntry {
        id = 3,
        optionsCount = 7500,
        dateAwarded = DateTime.Parse("1998/09/30") },
      new StudentOptionEntry {
        id = 4,
        optionsCount = 2456,
        dateAwarded = DateTime.Parse("1997/12/31") },
      new StudentOptionEntry {
        id = 101,
        optionsCount = 2,
        dateAwarded = DateTime.Parse("1998/12/31") }
    };

        return (empOptions);
    }
}

Result

Related Topic