linq
  1. linq-grouping-results

Grouping Results in LINQ

In LINQ, grouping is a way to categorize data based on specific criteria. It is useful when we want to group data by specific properties, and apply aggregate functions on grouped data. In this tutorial, we will learn how to group data using LINQ.

Syntax

var query = from item in collection
            group item by item.SomeProperty into grouping
            select new { grouping.Key, Count = grouping.Count(), Sum = grouping.Sum(x => x.SomeOtherProperty)};

Example

Let's say we have a collection of products, and we want to group them by their category and check the total number of products and the average price of products in each category. We can achieve this using the following code:

class Product
{
    public string Name { get; set; }
    public string Category { get; set; }
    public int Price { get; set; }
}

List<Product> products = new List<Product>
{
    new Product { Name = "Laptop", Category = "Electronics", Price = 1000 },
    new Product { Name = "Phone", Category = "Electronics", Price = 500 },
    new Product { Name = "Book", Category = "Stationary", Price = 20 },
    new Product { Name = "Chair", Category = "Furniture", Price = 50 },
    new Product { Name = "Table", Category = "Furniture", Price = 200 }
};

var result = from product in products
             group product by product.Category into g
             select new
             {
                 CategoryName = g.Key,
                 TotalProducts = g.Count(),
                 AveragePrice = g.Average(p => p.Price)
             };

In the above example, we have a collection of Products, and we're grouping them by Category using LINQ. We're calculating the Total Number of Products and the Average Price of Products in each Category.

Output

After running the above code, the output will be:

CategoryName: Electronics, TotalProducts: 2, AveragePrice: 750
CategoryName: Stationary, TotalProducts: 1, AveragePrice: 20
CategoryName: Furniture, TotalProducts: 2, AveragePrice: 125

Explanation

In the above example, we used the group by clause to group the products by the Category property. The grouped result is then projected using the select clause, where we're selecting the CategoryName, TotalProducts, and AveragePrice using various aggregate functions (Count, Average).

Use

Grouping is used when we want to categorize data based on specific criteria, and applying aggregate functions on grouped data. It is useful in scenarios where we want to group data by several properties, and calculate the sum, count, or average of some other property in each group.

Important Points

  • The group by clause is used to group data in LINQ.
  • After grouping data, we can apply various aggregate functions like Count, Average, and Sum to calculate the summary of each group's data.
  • We can also use multiple properties to group data using the anonymous object syntax in the group by clause.

Summary

In this tutorial, we learned about grouping data using LINQ. We've seen how we can group data by specific properties and apply aggregate functions on grouped data. We also saw an example of grouping Products by Category and calculating the Total Number of Products and the Average Price of Products in each Category.

Published on: