Open In App

LINQ | Grouping Operator | GroupBy

In LINQ, grouping operators pick the elements of the sequence or collection which contains common attributes and serve them in a group. Or in other words, we can say that the grouping operator returns the group of elements based on the given key. This group is held in a special type of collection, which implements the IGrouping<TKey, TElement> interface, where TKey is the key through which the group is created and TElement is the collection of elements which peers with the grouping key value. For example, a sequence contains 6 elements, i.e, Abc, cvd, Abc, Abc, ert, Por,. Now we want a group which contains all the Abc present in that sequence. So Abc is the key through which we create another group that contains 3 Abc. As shown in the below image:



The Standard Query Operator contains 2 different types of grouping operators:

  1. GroupBy
  2. ToLookup

GroupBy Operator

The working of the GroupBy operator is similar to the SQL GroupBy clause. It is used to return the group of elements which share the common attributes or key from the given sequence or collection. Every group is represented by IGrouping<TKey, TElement> object.



Important Points:

Example 1:




// C# program to divide the employees
// in groups according to their salary
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
  
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                    emp_hire_date = "12/3/2017", emp_salary = 20000},
  
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to divide the employees
        // in the groups according to 
        // their salary using GroupBy 
        // operator in query syntax
        var res = from e in emp
                    group e by e.emp_salary;
  
        foreach(var val in res)
        {
              
            // Here salary is the key value
            Console.WriteLine("Group By Salary: {0}", val.Key);
  
            // Display name of the employees
            // Inner collection according to
            // the key value
            foreach(Employee e in val)
            {
                Console.WriteLine("Employee Name: {0}",
                                           e.emp_name);
            }
        }
    }
}

Output:
Group By Salary: 20000
Employee Name: Anjita
Group By Salary: 30000
Employee Name: Soniya
Group By Salary: 40000
Employee Name: Rohit
Employee Name: Supriya
Employee Name: Anil
Group By Salary: 50000
Employee Name: Anju

Example 2:




// C# program to divide the employees
// in the groups according to their
// language
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
    public string emp_lang
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
  
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                 emp_hire_date = "12/3/2017", emp_salary = 20000, emp_lang = "Ruby"},
  
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                 emp_hire_date = "22/4/2018", emp_salary = 30000, emp_lang = "Java"},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
               emp_hire_date = "3/5/2016", emp_salary = 40000, emp_lang = "Perl"},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                     emp_hire_date = "4/8/2017", emp_salary = 40000, emp_lang = "Java"},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
               emp_hire_date = "12/1/2016", emp_salary = 40000, emp_lang = "C#"},
  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                 emp_hire_date = "17/6/2015", emp_salary = 50000, emp_lang = "C#"},
        };
  
        // Query to divide the employees in 
        // the groups according to their 
        // language using GroupBy method in
        // the method syntax
        var res = emp.GroupBy(e => e.emp_lang);
  
        foreach(var val in res)
        {
              
            // Here language is the key value
            Console.WriteLine("Group By Language: {0}", val.Key);
  
            // Display name of the employees
            // Inner collection according to
            // the key value
            foreach(Employee e in val)
            {
                Console.WriteLine("Employee Name: {0}", e.emp_name);
            }
        }
    }
}

Output:
Group By Language: Ruby
Employee Name: Anjita
Group By Language: Java
Employee Name: Soniya
Employee Name: Supriya
Group By Language: Perl
Employee Name: Rohit
Group By Language: C#
Employee Name: Anil
Employee Name: Anju

Article Tags :
C#