LINQ | Grouping Operator | GroupBy
Last Updated :
26 May, 2019
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:
- GroupBy
- 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:
- It support query syntax in both C# and VB.Net languages. As shown in example 1.
- You can also use into with GroupBy in C# query. The into keyword allows you to continue with the query and can perform more query operation. Or in other words, it is a temporary identifier which allows you to perform additional query operation.
- You can also use Into Group with GroupBy in VB.Net.
- LINQ query is ended with the help Select or Groupby clause.
- It can also support method syntax in both C# and VB.Net languages. As shown in example 2.
- It present in both the Queryable and Enumerable class. And in these classes, the GroupBy method is overloaded in eight different types.
- It is implemented by using deferred execution.
- Key value can be of any type either string, int, float, anonymous, bool, etc. depends upon the requirement of the user.
- The group return by this operator can contain zero or more elements that match the key value.
Example 1:
CSharp
// 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:
CSharp
// 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
Similar Reads
LINQ | Grouping Operator | ToLooKUp 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, w
3 min read
LINQ | Filtering Operator | where Filtering operators are those operators which are used to filter the data according to the user requirement from the given data source or from the given sequence. For example, in an employee record, we want to get the data of the employees whose age in 21. So, we filter the record, according to thei
5 min read
LINQ | Filtering Operator | OfType Filtering operators are those operators which are used to filter the data according to the user requirement from the given data source or from the given sequence. For example, in an employee record, we want to get the data of the employees whose age in 21. So, we filter the record, according to thei
3 min read
LINQ | Concatenation Operator | Concat The concatenation is a process in which one sequence is appended into another sequence. In LINQ, the concatenation operation contains only one operator that is known as Concat. It is used to append two same types of sequences or collections and return a new sequence or collection. It does not suppor
3 min read
LINQ | Set Operator | Distinct In LINQ, Set operators are those operators in query expression which return a result set which is based on the existence or non-existence of the equivalent elements within the same or different collections or sequences or sets. The standard query operator contains the following set operators: Union
3 min read
LINQ | Aggregate Operators | LongCount In LINQ, a Count operator is available to count the elements of the given sequence or collection. But there is a drawback of count operator, i.e, it only works for the MaxValue, i.e, 2147483647, if you try to more than this value then it will throw an OverflowException. So this drawback is overcome
3 min read