In this tutorial, we will learn about Grouping operators - GroupBy & ToLookup in LINQ.
In LINQ, grouping operators does the same thing as SQL’s GROUP BY clause in SQL query. In this tutorial, we will learn about the sorting operator - GroupBy & ToLookup.
The GroupBy() method does the same thing as the Group By clause in SQL query. The GroupBy() used to group the collection items based on the specified key and it returns the collection of IGrouping<Key, Values>.
The GroupBy() method has 8 overload methods. You can learn more about GroupBy() overload methods
In this example, we create a group of employees who have the same salary. Employees who has the same salary are in the same collection and each grouped collection has the key and collection, where the key will be the salary and the collection which contains the Employee data whose salary matches the key. For example:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqPrograms
{
internal class Program
{
public static void Main(string[] args)
{
// Employee collection
IList<Employee> employeelist = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 250000 } ,
new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 180000 },
new Employee() { EmpID = 6, EmpName = "Bill" , City = "Chicago", Salary = 250000 }
};
// LINQ Query Syntax to GroupBy Data based on Salary
Console.WriteLine("--Employee Grouped by their Salary--");
var GroupByResult = from emp in employeelist
group emp by emp.Salary;
foreach (var empSalary in GroupByResult)
{
Console.WriteLine($"Salary group: {empSalary.Key}");
foreach(var emp in empSalary )
{
Console.WriteLine($"Employee Name: {emp.EmpName}; City: {emp.City};");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
public int Salary { get; set; }
}
}
--Employee Grouped by their Salary--
Salary group: 130000
Employee Name: John; City: New York;
Salary group: 250000
Employee Name: Moin; City: New Orleans;
Employee Name: Bill; City: Chicago;
Salary group: 180000
Employee Name: Bill; City: Seattle;
Employee Name: Ron; City: San Jose;
Salary group: 200000
Employee Name: Ram; City: Delhi;
The GroupBy() extension method works the same way in the method syntax as in query syntax. In the method syntax, you need to specify the lambda expression for the specified key field in the Groupby() extension method. Here is the example of GroupBy() method with method Syntax:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqPrograms
{
internal class Program
{
public static void Main(string[] args)
{
// Employee collection
IList<Employee> employeelist = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 250000 } ,
new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 180000 },
new Employee() { EmpID = 6, EmpName = "Bill" , City = "Chicago", Salary = 250000 }
};
// LINQ Method Syntax to GroupBy Data based on Salary
Console.WriteLine("--Employee Grouped by their Salary--");
var GroupByResult = employeelist.GroupBy(x => x.Salary);
foreach (var empSalary in GroupByResult)
{
Console.WriteLine($"Salary group: {empSalary.Key}");
foreach(var emp in empSalary )
{
Console.WriteLine($"Employee Name: {emp.EmpName}; City: {emp.City};");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
public int Salary { get; set; }
}
}
--Employee Grouped by their Salary--
Salary group: 130000
Employee Name: John; City: New York;
Salary group: 250000
Employee Name: Moin; City: New Orleans;
Employee Name: Bill; City: Chicago;
Salary group: 180000
Employee Name: Bill; City: Seattle;
Employee Name: Ron; City: San Jose;
Salary group: 200000
Employee Name: Ram; City: Delhi;
The ToLookup() extension method works the same as the Groupby() method. The only difference between both the methods is execution of methods. The GroupBy() method execution is deferred whereas the ToLookup() method execution is immediate.
The ToLookup() method is not supported in the query syntax.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqPrograms
{
internal class Program
{
public static void Main(string[] args)
{
// Employee collection
IList<Employee> employeelist = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 250000 } ,
new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 180000 },
new Employee() { EmpID = 6, EmpName = "Bill" , City = "Chicago", Salary = 250000 }
};
// LINQ Method Syntax to GroupBy Data based on Salary
Console.WriteLine("--Employee Grouped by their Salary--");
var GroupByResult = employeelist.ToLookup(x => x.Salary);
foreach (var empSalary in GroupByResult)
{
Console.WriteLine($"Salary group: {empSalary.Key}");
foreach(var emp in empSalary )
{
Console.WriteLine($"Employee Name: {emp.EmpName}; City: {emp.City};");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
public int Salary { get; set; }
}
}
--Employee Grouped by their Salary--
Salary group: 130000
Employee Name: John; City: New York;
Salary group: 250000
Employee Name: Moin; City: New Orleans;
Employee Name: Bill; City: Chicago;
Salary group: 180000
Employee Name: Bill; City: Seattle;
Employee Name: Ron; City: San Jose;
Salary group: 200000
Employee Name: Ram; City: Delhi;
Learn Joining Operator - Join in the next tutorial.