Dotnet-StuffWorld

Monday, April 13, 2009

 

Some Common GroupBy Operations on List<> using LINQ

Some Common GroupBy Operations on List<> using LINQ:
The ‘GroupBy’ feature in LINQ is amazing and very powerful. When you use a ‘GroupBy’ in LINQ, internally it calls an extension method which returns a sequence of System.Collections.Generic.IEnumerable<(Of <(IGrouping<(Of <(TKey, TSource>)>)>)>)
The GroupBy<(Of <(TSource, TKey>)>)(IEnumerable<(Of <(TSource>)>), Func<(Of <(TSource, TKey>)>)) method returns a collection of IGrouping<(Of <(TKey, TElement>)>) objects, one for each distinct key that was encountered. The key represents the attribute that is common to each value in the IGrouping<(Of <(TKey, TElement>)>) and can be accessed using a ForEach loop.
In order to understand GroupBy in LINQ, let’s take an example. Linda is an HR in a small private firm. To facilitate the HR process, she wants a simple console application to obtain some quick results. She needs the following details of Employees:
- Raw List of Employees
- List of Employees grouped by the first letter of their FirstName
- List of employees grouped by the Year in which they were born
- List of employees grouped by the Year and Month in which they were born
- Total count of employees having Birthdays in the same Year
- Sex Ratio
Let’s take these requirements one by one and see how they can be easily achieved using the ‘GroupBy’ in LINQ. We will first create a simple list of employees (List) and add some data to it.
C#
class Program
{
static void Main(string[] args)
{
List empList = new List();
empList.Add(new Employee() { ID = 1, FName = "John", MName = "", LName = "Shields", DOB = DateTime.Parse("12/11/1971"), Sex = 'M' });
empList.Add(new Employee() { ID = 2, FName = "Mary", MName = "Matthew", LName = "Jacobs", DOB = DateTime.Parse("01/17/1961"), Sex = 'F' });
empList.Add(new Employee() { ID = 3, FName = "Amber", MName = "Carl", LName = "Agar", DOB = DateTime.Parse("12/23/1971"), Sex = 'M' });
empList.Add(new Employee() { ID = 4, FName = "Kathy", MName = "", LName = "Berry", DOB = DateTime.Parse("11/15/1976"), Sex = 'F' });
empList.Add(new Employee() { ID = 5, FName = "Lena", MName = "Ashco", LName = "Bilton", DOB = DateTime.Parse("05/11/1978"), Sex = 'F' });
empList.Add(new Employee() { ID = 6, FName = "Susanne", MName = "", LName = "Buck", DOB = DateTime.Parse("03/7/1965"), Sex = 'F' });
empList.Add(new Employee() { ID = 7, FName = "Jim", MName = "", LName = "Brown", DOB = DateTime.Parse("09/11/1972"), Sex = 'M' });
empList.Add(new Employee() { ID = 8, FName = "Jane", MName = "G", LName = "Hooks", DOB = DateTime.Parse("12/11/1972"), Sex = 'F' });
empList.Add(new Employee() { ID = 9, FName = "Robert", MName = "", LName = "", DOB = DateTime.Parse("06/28/1964"), Sex = 'M' });
empList.Add(new Employee() { ID = 10, FName = "Cindy", MName = "Preston", LName = "Fox", DOB = DateTime.Parse("01/11/1978"), Sex = 'M' });

// Printing the List
Console.WriteLine("\n{0,2} {1,7} {2,8} {3,8} {4,23} {5,3}",
"ID", "FName", "MName", "LName", "DOB", "Sex");
empList.ForEach(delegate(Employee e)
{
Console.WriteLine("{0,2} {1,7} {2,8} {3,8} {4,23} {5,3}",
e.ID, e.FName, e.MName, e.LName, e.DOB, e.Sex);
});

Console.ReadLine();

}

class Employee
{
public int ID { get; set; }
public string FName { get; set; }
public string MName { get; set; }
public string LName { get; set; }
public DateTime DOB { get; set; }
public char Sex { get; set; }
}


VB.NET
Module Module1

Sub Main()
Dim empList As New List(Of Employee)()
empList.Add(New Employee() With {.ID = 1, .FName = "John", .MName = "", .LName = "Shields", .DOB = DateTime.Parse("12/11/1971"), .Sex = "M"c})
empList.Add(New Employee() With {.ID = 2, .FName = "Mary", .MName = "Matthew", .LName = "Jacobs", .DOB = DateTime.Parse("01/17/1961"), .Sex = "F"c})
empList.Add(New Employee() With {.ID = 3, .FName = "Amber", .MName = "Carl", .LName = "Agar", .DOB = DateTime.Parse("12/23/1971"), .Sex = "M"c})
empList.Add(New Employee() With {.ID = 4, .FName = "Kathy", .MName = "", .LName = "Berry", .DOB = DateTime.Parse("11/15/1976"), .Sex = "F"c})
empList.Add(New Employee() With {.ID = 5, .FName = "Lena", .MName = "Ashco", .LName = "Bilton", .DOB = DateTime.Parse("05/11/1978"), .Sex = "F"c})
empList.Add(New Employee() With {.ID = 6, .FName = "Susanne", .MName = "", .LName = "Buck", .DOB = DateTime.Parse("03/7/1965"), .Sex = "F"c})
empList.Add(New Employee() With {.ID = 7, .FName = "Jim", .MName = "", .LName = "Brown", .DOB = DateTime.Parse("09/11/1972"), .Sex = "M"c})
empList.Add(New Employee() With {.ID = 8, .FName = "Jane", .MName = "G", .LName = "Hooks", .DOB = DateTime.Parse("12/11/1972"), .Sex = "F"c})
empList.Add(New Employee() With {.ID = 9, .FName = "Robert", .MName = "", .LName = "", .DOB = DateTime.Parse("06/28/1964"), .Sex = "M"c})
empList.Add(New Employee() With {.ID = 10, .FName = "Cindy", .MName = "Preston", .LName = "Fox", .DOB = DateTime.Parse("01/11/1978"), .Sex = "M"c})

' Printing the List
Console.WriteLine(Constants.vbLf & "{0,2} {1,7} {2,8} {3,8} {4,23} {5,3}", "ID", "FName", "MName", "LName", "DOB", "Sex")
empList.ForEach(AddressOf AnonymousMethod1)

Console.ReadLine()
End Sub

Private Sub AnonymousMethod1(ByVal e As Employee)
Console.WriteLine("{0,2} {1,7} {2,8} {3,8} {4,23} {5,3}", e.ID, e.FName, e.MName, e.LName, e.DOB, e.Sex)
End Sub

End Module

Friend Class Employee
Private privateID As Integer
Public Property ID() As Integer
Get
Return privateID
End Get
Set(ByVal value As Integer)
privateID = value
End Set
End Property
Private privateFName As String
Public Property FName() As String
Get
Return privateFName
End Get
Set(ByVal value As String)
privateFName = value
End Set
End Property
Private privateMName As String
Public Property MName() As String
Get
Return privateMName
End Get
Set(ByVal value As String)
privateMName = value
End Set
End Property
Private privateLName As String
Public Property LName() As String
Get
Return privateLName
End Get
Set(ByVal value As String)
privateLName = value
End Set
End Property
Private privateDOB As DateTime
Public Property DOB() As DateTime
Get
Return privateDOB
End Get
Set(ByVal value As DateTime)
privateDOB = value
End Set
End Property
Private privateSex As Char
Public Property Sex() As Char
Get
Return privateSex
End Get
Set(ByVal value As Char)
privateSex = value
End Set
End Property
End Class

http://www.dotnetcurry.com/ShowArticle.aspx?ID=260

Comments:

Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

April 2009   July 2009   August 2009   September 2009   September 2010   February 2011  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]