linq
  1. linq-grouping-data

Grouping Data in LINQ Query Syntax

LINQ provides a GroupBy method that allows us to group data in a collection based on one or more fields. Grouping data is useful when we want to summarize or aggregate data based on certain criteria. In this tutorial, we will learn how to group data in LINQ Query Syntax.

Syntax

var result = from x in collection
             group x by x.FieldName into g
             select new 
             {
                 GroupName = g.Key,
                 Count = g.Count(),
                 MaxValue = g.Max(x => x.FieldValue),
                 MinValue = g.Min(x => x.FieldValue),
                 AverageValue = g.Average(x => x.FieldValue),
                 SumValue = g.Sum(x => x.FieldValue)
             };

In the above syntax, the group by clause is used to group data based on one or more fields, and the into keyword is used to define a new range variable that represents the group. The g variable is a grouping that contains all the items in the collection that share the same field value. The select clause is used to define the output of the query.

Example

Let's say we have a collection of Person objects that have Name, Age, and Gender fields. We want to group the data by Gender and get the count, max, min, average, and sum of ages for each group.

List<Person> people = new List<Person>()
{
    new Person() { Name = "John", Age = 25, Gender = "Male" },
    new Person() { Name = "Rachel", Age = 30, Gender = "Female" },
    new Person() { Name = "Mark", Age = 36, Gender = "Male" },
    new Person() { Name = "Emily", Age = 27, Gender = "Female" },
    new Person() { Name = "Andrew", Age = 34, Gender = "Male" },
    new Person() { Name = "Grace", Age = 29, Gender = "Female" }
};

var result = from p in people
             group p by p.Gender into g
             select new
             {
                 Gender = g.Key,
                 Count = g.Count(),
                 MaxAge = g.Max(p => p.Age),
                 MinAge = g.Min(p => p.Age),
                 AverageAge = g.Average(p => p.Age),
                 SumAge = g.Sum(p => p.Age)
             };

In the above example, we used the LINQ group by clause to group the data by Gender. After grouping, we selected a new object that contains the Gender, Count, MaxAge, MinAge, AverageAge, and SumAge properties for each group.

Output

After running the above code, the output will be:

Gender: Male, Count: 3, MaxAge: 36, MinAge: 25, AverageAge: 31.67, SumAge: 95
Gender: Female, Count: 3, MaxAge: 30, MinAge: 27, AverageAge: 28.67, SumAge: 86

The output shows that the data is grouped by Gender, and for each group, we get the Count, MaxAge, MinAge, AverageAge, and SumAge.

Explanation

In the syntax example, we used the group by clause to group the data by FieldName, where FieldName is the name of the field on which we want to group the data. We used the into keyword to define a new range variable g, which represents the group. The g variable is an IEnumerable of items that share the same FieldName value.

In the select clause, we created a new object that contains the group's FieldName value, Count, MaxValue, MinValue, AverageValue, and SumValue.

Use

Grouping data is useful when we want to summarize or aggregate data based on certain criteria. For example, we can group data by date, category, or any other criteria and then get the count, max, min, average, or sum of data in each group.

Important Points

  • The group by clause is used to group data in LINQ Query Syntax.
  • The into keyword is used to define a new range variable that represents the group.
  • The Key property of the grouping represents the FieldName value on which the data is grouped.
  • We can use aggregate functions like Count, Max, Min, Average, and Sum on the grouped data to get summary information.

Summary

In this tutorial, we learned how to group data in LINQ Query Syntax. We looked at the syntax, an example, and the output of a LINQ Query that groups data based on one or more fields. We also discussed the use cases and important points related to grouping data in LINQ.

Published on: