C# > Advanced C# > LINQ > Aggregation (sum, avg, count)
LINQ Aggregation with Grouping: Summing Grouped Data
This snippet demonstrates how to use LINQ to group data and then calculate aggregate values (sum, average, count) for each group. This is a powerful technique for summarizing data by category.
Grouping and Aggregating
This code snippet demonstrates grouping a list of products by their name and then calculating the total revenue, average price, and count for each product. The `GroupBy` method groups the products by their `ProductName`. The `Select` method then creates a new anonymous object for each group, containing the `ProductName`, `TotalRevenue` (calculated using `Sum`), `AveragePrice` (calculated using `Average`), and `Count` (calculated using `Count`). The results are then printed to the console.
using System;
using System.Collections.Generic;
using System.Linq;
public class GroupedAggregationExample
{
public static void Main(string[] args)
{
// Sample data: Product name and price
List<(string ProductName, decimal Price)> products = new List<(string, decimal)>
{
("Apple", 1.00m),
("Banana", 0.75m),
("Apple", 1.25m),
("Orange", 0.90m),
("Banana", 0.80m),
("Apple", 1.10m)
};
// Group products by name and calculate the sum of prices for each product
var groupedProducts = products
.GroupBy(p => p.ProductName)
.Select(g => new
{
ProductName = g.Key,
TotalRevenue = g.Sum(p => p.Price),
AveragePrice = g.Average(p => p.Price),
Count = g.Count()
});
// Print the results
foreach (var product in groupedProducts)
{
Console.WriteLine($"Product: {product.ProductName}, Total Revenue: {product.TotalRevenue}, Average Price: {product.AveragePrice}, Count: {product.Count}");
}
}
}
Explanation of Concepts
The `GroupBy` operator is a fundamental LINQ operator that divides a sequence into groups based on a specified key. It returns an `IEnumerable
Real-Life Use Case
Consider a database of customer orders. You could group the orders by customer ID and then calculate the total amount spent by each customer, the average order value, and the number of orders placed. This information is invaluable for customer segmentation, targeted marketing, and loyalty programs.
Best Practices
Interview Tip
Be prepared to explain the difference between `GroupBy` and other LINQ operators like `Where` and `Select`. Understand how to use aggregation functions within the `Select` statement after grouping. Be ready to discuss the performance implications of grouping large datasets.
When to Use Them
Use grouped aggregation when you need to analyze data based on categories or groups. This is commonly used in reporting, data analysis, and summarizing information based on different dimensions.
Memory Footprint
Grouping operations can have a significant memory footprint, especially when dealing with large datasets and complex grouping criteria. The `GroupBy` operator might need to store intermediate results in memory before performing the aggregation. Consider using streaming grouping techniques or database-side aggregation to reduce memory consumption when working with extremely large datasets.
Alternatives
Pros
Cons
FAQ
-
What happens if a group is empty?
If a group is empty, `Sum` will return 0. `Average` will return 0 if the type is a value type, and null if the type is a nullable type. `Count` will return 0. -
Can I group by multiple properties?
Yes, you can group by multiple properties by creating an anonymous type as the key in the `GroupBy` method (e.g., `GroupBy(p => new { p.ProductName, p.Category })`). -
Is the order of the groups preserved?
The order of the groups is generally preserved based on the order of the elements in the original sequence. However, you should not rely on this behavior if order is critical; explicitly order the results if necessary.