LINQ Interview Question #60
How do you calculate aggregates for each LINQ group?
Joins, Grouping, Set & Quantifier Operators Mid-Level Intermediate
Quick Interview Answer
After GroupBy, project each group with Select and call aggregate operators such as Count, Sum, Average, Min, or Max on that group.
Detailed Explanation
After GroupBy, project each group with Select and call aggregate operators such as Count, Sum, Average, Min, or Max on that group.
This pattern is common for reporting and database aggregation queries.
Code Example
var summary = orders
.GroupBy(o => o.CustomerId)
.Select(g => new
{
CustomerId = g.Key,
OrderCount = g.Count(),
TotalAmount = g.Sum(o => o.Total)
});