LINQ GroupBy Method


In LINQ, GroupBy operator is used to group list/collection items based on specified key value and it returns a collection of IGrouping<Key, Values>. The groupby method in LINQ is same as the SQL group by statement. 

Syntax of LINQ GroupBy Method

Following is the syntax of using LINQ GroupBy method to group elements based on the specified key value.

 

C# Code

 

var student = objStudent.GroupBy(x => x.Location);

VB.NET Code

 

Dim student = objStudent.GroupBy(Function(x) x.Location)

If you observe above syntax we are grouping "objStudent" collection items based on student location.

Example of LINQ GroupBy in Method Syntax

Following is the example of using LINQ GroupBy in method syntax.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

staticvoid Main(string[] args)

{

List<Student> objStudent = new List<Student>()

{

new Student() { Name = "Suresh Dasari", Gender = "Male",Location="Chennai" },

new Student() { Name = "Rohini Alavala", Gender = "Female", Location="Chennai" },

new Student() { Name = "Praveen Alavala", Gender = "Male",Location="Bangalore" },

new Student() { Name = "Sateesh Alavala", Gender = "Male", Location ="Vizag"},

new Student() { Name = "Madhav Sai", Gender = "Male", Location="Nagpur"}

};

var student = objStudent.GroupBy(x => x.Location);

foreach (var sitem in student)

{

Console.WriteLine(sitem.Key, sitem.Count());

Console.WriteLine();

foreach (var stud in sitem)

{

Console.WriteLine(stud.Name + "\t" + stud.Location);

}

Console.WriteLine();

}

Console.ReadLine();

}

}

class Student

{

public string Name { getset; }

public string Gender { getset; }

public string Location { getset; }

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim objStudent As New List(Of Student)() From {

New Student() With {.Name = "Suresh Dasari", .Gender = "Male", .Location = "Chennai"},

New Student() With {.Name = "Rohini Alavala", .Gender = "Female", .Location = "Chennai"},

New Student() With {.Name = "Praveen Alavala", .Gender = "Male", .Location = "Bangalore"},

New Student() With {.Name = "Sateesh Alavala", .Gender = "Male", .Location = "Vizag"},

New Student() With {.Name = "Madhav Sai", .Gender = "Male", .Location = "Nagpur"}

}

Dim student = objStudent.GroupBy(Function(x) x.Location)

For Each sitem In student

Console.WriteLine(sitem.Key, sitem.Count())

Console.WriteLine()

For Each stud In sitem

Console.WriteLine(stud.Name + vbTab + stud.Location)

Next

Console.WriteLine()

Next

Console.ReadLine()

EndSub

 

Class Student

Public Property Name() As String

Get

Return m_Name

End Get

Set(ByVal value As String)

m_Name = value

End Set

End Property

Private m_Name As String

Public Property Gender() As String

Get

Return m_Gender

End Get

Set(ByVal value As String)

m_Gender = value

End Set

End Property

Private m_Gender As String

Public Property Location() As String

Get

Return m_Location

End Get

Set(ByVal value As String)

m_Location = value

End Set

End Property

Private m_Location As String

End Class

End Module

If you observe the above example we are grouping "objStudent" collection items based on student location.

Output of LINQ GroupBy in Method Syntax

Following is the result of LINQ GroupBy in method syntax example.

 

Chennai

 

Suresh Dasari Chennai

Rohini Alavala Chennai

 

Bangalore

 

Praveen Alavala Bangalore

 

Vizag

 

Sateesh Alavala Vizag

 

Nagpur

 

Madhav Sai Nagpur

Example of LINQ GroupBy in Query Syntax

Following is the example of using LINQ GroupBy method in query syntax.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

namespace Linqtutorials

{

class Program

{

staticvoid Main(string[] args)

{

List<Student> objStudent = new List<Student>()

{

new Student() { Name = "Suresh Dasari", Gender = "Male",Location="Chennai" },

new Student() { Name = "Rohini Alavala", Gender = "Female", Location="Chennai" },

new Student() { Name = "Praveen Alavala", Gender = "Male",Location="Bangalore" },

new Student() { Name = "Sateesh Alavala", Gender = "Male", Location ="Vizag"},

new Student() { Name = "Madhav Sai", Gender = "Male", Location="Nagpur"}

};

var student = from std in objStudent

group std by std.Location;

foreach (var sitem in student)

{

Console.WriteLine(sitem.Key, sitem.Count());

Console.WriteLine();

foreach (var stud in sitem)

{

Console.WriteLine(stud.Name + "\t" + stud.Location);

}

Console.WriteLine();

}

Console.ReadLine();

}

}

class Student

{

public string Name { getset; }

public string Gender { getset; }

public string Location { getset; }

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim objStudent As New List(Of Student)() From {

New Student() With {.Name = "Suresh Dasari", .Gender = "Male", .Location = "Chennai"},

New Student() With {.Name = "Rohini Alavala", .Gender = "Female", .Location = "Chennai"},

New Student() With {.Name = "Praveen Alavala", .Gender = "Male", .Location = "Bangalore"},

New Student() With {.Name = "Sateesh Alavala", .Gender = "Male", .Location = "Vizag"},

New Student() With {.Name = "Madhav Sai", .Gender = "Male", .Location = "Nagpur"}

}

Dim objVals = From std In objStudent Group By std.Location Into Group

For Each sitem In objVals

Console.WriteLine(sitem.Location)

Console.WriteLine()

For Each stud In sitem.Group

Console.WriteLine(stud.Name + vbTab + stud.Location)

Next

Console.WriteLine()

Next

Console.ReadLine()

EndSub

 

Class Student

Public Property Name() As String

Get

Return m_Name

End Get

Set(ByVal value As String)

m_Name = value

End Set

End Property

Private m_Name As String

Public Property Gender() As String

Get

Return m_Gender

End Get

Set(ByVal value As String)

m_Gender = value

End Set

End Property

Private m_Gender As String

Public Property Location() As String

Get

Return m_Location

End Get

Set(ByVal value As String)

m_Location = value

End Set

End Property

Private m_Location As String

End Class

End Module