linq
  1. linq-aggregating-data

Aggregating Data with LINQ

LINQ (Language-Integrated Query) is a powerful tool that allows querying data from various data sources like SQL Server, XML documents, and collections. In this article, we'll explore how LINQ can aggregate data using methods like GroupBy(), Sum(), Count(), etc.

Syntax

The syntax for aggregating data using LINQ depends on the method being used. Here is an example of using GroupBy() method to group data by a common property:

var results = from item in collection
              group item by item.Property into g
              select new
              {
                  Property = g.Key,
                  Count = g.Count(),
                  Sum = g.Sum(x => x.Amount)
              };

In the above syntax, we are grouping items in the collection based on a common property and selecting some aggregate values like total count and sum.

Example

Let's consider an example where we have a collection of sales data and we want to aggregate the data based on a common property like product category.

public class SalesData
{
    public string Product { get; set; }
    public string Category { get; set; }
    public decimal Amount { get; set; }
}

List<SalesData> salesData = new List<SalesData>()
{
    new SalesData() { Product = "Product A", Category = "Electronics", Amount = 1000 },
    new SalesData() { Product = "Product B", Category = "Clothing", Amount = 500 },
    new SalesData() { Product = "Product C", Category = "Electronics", Amount = 1500 },
    new SalesData() { Product = "Product D", Category = "Clothing", Amount = 750 },
    new SalesData() { Product = "Product E", Category = "Electronics", Amount = 2000 },
};

Now, let's aggregate the data by category and calculate the sum and count of sales.

var results = from item in salesData
              group item by item.Category into g
              select new
              {
                  Category = g.Key,
                  Count = g.Count(),
                  Sum = g.Sum(x => x.Amount)
              };

Output

After running the above code, the output will be,

Category        Count       Sum
Electronics     3           4500
Clothing        2           1250

Explanation

In the above example, we grouped sales data into two groups based on the product category using the GroupBy() method. Then we selected some aggregate values like count and sum of sales using Count() and Sum() methods respectively.

Use

Aggregating data with LINQ is useful when we have to summarize data and extract insights. We can calculate various aggregate values like sum, count, average, and minimum/maximum values based on certain criteria.

Important Points

  • GroupBy() method is used to group data based on a common property.
  • Sum(), Count(), Min(), and Max() methods are used to calculate aggregate values like total amount, count, etc.
  • Aggregating data can be useful in data analysis, reporting, and automated decision-making.

Summary

In this article, we explored how to aggregate data with LINQ using methods like GroupBy(), Sum(), Count(), etc. We've seen an example of calculating the count and sum of sales data based on a common property.LINQ makes it easier to aggregate data based on various criteria, making data analysis and reporting more manageable.

Published on: