LINQ GroupBy Method

  Total Views : 215
  Comments : 0
 
  Keywords : How to use linq groupby method in c# / vb.net with example, Linq groupby method to group list / collection items based on key value in c# / vb.net, Linq groupby() operator with example

Here we will learn linq groupby in method syntax and query syntax in c#, vb.net with example and how to use linq groupby method to group list / collection items based on specified key value in method syntax and query syntax in c#, vb.net with example.

LINQ GroupBy Method / Operator

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 SQL group by statement. 

Syntax of LINQ GroupBy Method

Following is the syntax of using LINQ GroupBy method to group elements based on 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 { 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()

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 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 { 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()

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

Output 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.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User