Author : Rahul
Last Modified : 13-Jul-2021
Complexity : Beginner

Grouping Operator in LINQ


Grouping Operator in LINQ is used to group the data based on some keys and return the collection of key-value pairs. Just like GroupBy is used in SQL to group the data. Below are some examples in this category.

 

Query to partition a list of numbers by their remainder when divided by 10.

class Program
 {
   static void Main(string[] args)
   {
     int[] words = { 10,15,18,26,6,5,8 };
     
     //Linq Query
     var result_Linq = from p in words group p by p%10 into g select new { Group_Key = g.Key, Group_Element = g };
     
     //Lambda Expression
     var result_Lambda = words.GroupBy(p => p%10).Select(g => new { Group_Key = g.Key, Group_Element = g });
     
     //Display Linq Query Result
     Console.WriteLine("Grouped List of Numbers (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Numbers with reminder {item.Group_Key}");
       foreach (var element in item.Group_Element)
       {
         Console.WriteLine(element);
       }
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Grouped List of Numbers (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine($"Numbers with reminder {item.Group_Key}");
       foreach (var element in item.Group_Element)
       {
         Console.WriteLine(element);
       }
     }
     
     Console.Read();
   }
 }

Output

Grouped List of Numbers (Using Linq):
Numbers with reminder 0
10
Numbers with reminder 5
15
5
Numbers with reminder 8
18
8
Numbers with reminder 6
26
6
Grouped List of Numbers (Using Lambda):
Numbers with reminder 0
10
Numbers with reminder 5
15
5
Numbers with reminder 8
18
8
Numbers with reminder 6
26
6

 


Query to partition a list of words by their first letter.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "ankit", "gopal", "kartik", "atul", "ramesh" };
     
     //Linq Query
     var result_Linq = from p in words group p by p[0] into g select new { Group_Key = g.Key, Group_Element = g };
     
     //Lambda Expression
     var result_Lambda = words.GroupBy(p => p[0]).Select(g => new { Group_Key = g.Key, Group_Element = g });
     
     //Display Linq Query Result
     Console.WriteLine("Grouped List of Words (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Words that starts with {item.Group_Key}");
       foreach (var element in item.Group_Element)
       {
         Console.WriteLine(element);
       }
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Grouped List of Words (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine($"Words that starts with {item.Group_Key}");
       foreach (var element in item.Group_Element)
       {
         Console.WriteLine(element);
       }
     }
     
     Console.Read();
   }
 }

Output

Grouped List of Words (Using Linq):
Words that starts with r
rahul
ramesh
Words that starts with a
ankit
atul
Words that starts with g
gopal
Words that starts with k
kartik
Grouped List of Words (Using Lambda):
Words that starts with r
rahul
ramesh
Words that starts with a
ankit
atul
Words that starts with g
gopal
Words that starts with k
kartik

 

Query to partition a list of employees by their department.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees group p by p.Emp_Department into g select new { Group_Key = g.Key, Group_Element = g };
     
     //Lambda Expression
     var result_Lambda = employees.GroupBy(p => p.Emp_Department).Select(g=>new { Group_Key = g.Key, Group_Element = g });
     
     //Display Linq Query Result
     Console.WriteLine("Grouped Employee Details (Using Linq):");
     foreach (var element in result_Linq)
     {
       Console.WriteLine($"Employees with Department {element.Group_Key}:");
       foreach (Employee item in element.Group_Element)
       {
         Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
           $"Employee Salary: {item.Emp_Salary}");
       }
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Grouped Employee Details (Using Lambda):");
     foreach (var element in result_Linq)
     {
       Console.WriteLine($"Employees with Department {element.Group_Key}:");
       foreach (Employee item in element.Group_Element)
       {
         Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
           $"Employee Salary: {item.Emp_Salary}");
       }
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Grouped Employee Details (Using Linq):
Employees with Department IT:
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Employees with Department HR:
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Employees with Department Account:
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Grouped Employee Details (Using Lambda):
Employees with Department IT:
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Employees with Department HR:
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Employees with Department Account:
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000