Grouping and Aggregating Data by Multiple Properties in C# Using LINQ

Madhawa Polkotuwa
4 min readSep 20, 2024

--

Grouping is a powerful feature that allows us to categorize and perform operations on specific sets of data.

intro

Introduction

In this post, we’ll explore how to group and aggregate data by multiple properties using LINQ in C#. Grouping is a powerful feature that allows us to categorize and perform operations on specific sets of data. We’ll use a dummy list of products to demonstrate how to group by both Category and PrductName and then calculate the average price for each group.

Defining the Product Class and Dummy List

We start by defining a simple Product class, which contains the properties ID, PrductName, Category, and Price. Next, we create a dummy list of products representing different categories such as 'Electronics', 'Clothing', and 'Books'.

public class Product
{
public int ID { get; set; }
public string PrductName { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}

List<Product> products = new List<Product>
{
new Product { ID = 1, PrductName = "Laptop", Category = "Electronics", Price = 299.99m },
new Product { ID = 2, PrductName = "Laptop", Category = "Electronics", Price = 99.99m },
new Product { ID = 3, PrductName = "Laptop", Category = "Electronics", Price = 299.00m },
new Product { ID = 4, PrductName = "Laptop", Category = "Electronics", Price = 199.99m },
new Product { ID = 5, PrductName = "Laptop", Category = "Electronics", Price = 499.99m },
new Product { ID = 6, PrductName = "Desktop", Category = "Electronics", Price = 324.00m },
new Product { ID = 7, PrductName = "Desktop", Category = "Electronics", Price = 690.00m },
new Product { ID = 8, PrductName = "Desktop", Category = "Electronics", Price = 450.00m },
new Product { ID = 9, PrductName = "Mouse", Category = "Electronics", Price = 25.10m },
new Product { ID = 10, PrductName = "TShirt", Category = "Clothing", Price = 49.99m },
new Product { ID = 11, PrductName = "TShirt", Category = "Clothing", Price = 29.99m },
new Product { ID = 12, PrductName = "TShirt", Category = "Clothing", Price = 39.99m },
new Product { ID = 13, PrductName = "IPhone", Category = "Electronics", Price = 199.99m },
new Product { ID = 14, PrductName = "Underware", Category = "Clothing", Price = 39.99m },
new Product { ID = 15, PrductName = "Harry Potter", Category = "Books", Price = 24.99m }
};

This dummy list helps simulate real-world data for grouping and aggregation.

Grouping and Aggregating Data by Multiple Properties

Now, we want to group the products by both their Category and PrductName and calculate the average price for each group. This can be achieved using the GroupBy method in LINQ.

Here’s how we do it:

var groupedData = products
.GroupBy(p => new { p.Category, p.PrductName }) // Grouping by Category and PrductName
.Select(g => new
{
GroupedProducts = g.Key,
AveragePrice = g.Average(p => p.Price) // Calculating the average price
});

foreach (var group in groupedData)
{
Console.WriteLine($"New Group : {group}");
}
New Group : { GroupedProducts = { Category = Electronics, PrductName = Laptop }, AveragePrice = 279.792 }
New Group : { GroupedProducts = { Category = Electronics, PrductName = Desktop }, AveragePrice = 488.00 }
New Group : { GroupedProducts = { Category = Electronics, PrductName = Mouse }, AveragePrice = 25.10 }
New Group : { GroupedProducts = { Category = Clothing, PrductName = TShirt }, AveragePrice = 39.99 }
New Group : { GroupedProducts = { Category = Electronics, PrductName = IPhone }, AveragePrice = 199.99 }
New Group : { GroupedProducts = { Category = Clothing, PrductName = Underware }, AveragePrice = 39.99 }
New Group : { GroupedProducts = { Category = Books, PrductName = Harry Potter }, AveragePrice = 24.99 }

Explanation

  • GroupBy(p => new { p.Category, p.PrductName }): This groups the products based on both the Category and PrductName properties.
  • Select(g => new { ... }): After grouping, we use Select to create a new anonymous object for each group, containing the grouped product properties and the average price.
  • g.Average(p => p.Price): This calculates the average price of the products in each group.

Why This Matters

This approach is useful when analyzing or summarizing data by multiple properties. For example, you might want to group sales data by both product and region or by product and salesperson. Grouping by multiple properties provides more granular insight, and combining it with aggregation functions such as Average, Sum, or Count gives you the ability to perform detailed analysis on your data.

demo video

Conclusion

Grouping and aggregating data by multiple properties using LINQ in C# is a simple yet powerful technique for data analysis. Whether you’re working with a list of products, sales data, or other types of collections, this approach helps you categorize and process the data efficiently.

--

--