In LINQ, the GroupBy operator is used to group list/collection items based on specified key values, 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.
LINQ GroupBy Syntax in C#
var student = objStudent.GroupBy(x => x.Location);
LINQ GroupBy Syntax in VB.NET
Dim student = objStudent.GroupBy(Function(x) x.Location)
If you observe above syntax we are grouping "objStudent" collection items based on student location.
LINQ GroupBy in Method Syntax Example
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
{
static void 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 { get; set; }
public string Gender { get; set; }
public string Location { get; set; }
}
}
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()
End Sub
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
The above example shows that we are grouping "objStudent" collection items based on student location.
Result 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 the LINQ GroupBy method in query syntax.
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
namespace Linqtutorials
{
class Program
{
static void 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 { get; set; }
public string Gender { get; set; }
public string Location { get; set; }
}
}
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()
End Sub
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
Result of LINQ GroupBy in Query Syntax
Following is the result of LINQ GroupBy in query syntax example.
Chennai
Suresh Dasari Chennai
Rohini Alavala Chennai
Bangalore
Praveen Alavala Bangalore
Vizag
Sateesh Alavala Vizag
Nagpur
Madhav Sai Nagpur
This is how we can use LINQ GroupBy in method syntax and query syntax to group list/collection items in c#, vb.net with examples.