Example:
var ordersByCustomer = dbContext.Orders
.GroupBy(o => o.CustomerId)
.Select(g => new {
CustomerId = g.Key,
TotalAmount = g.Sum(o => o.Amount),
AverageAmount = g.Average(o => o.Amount),
MaxAmount = g.Max(o => o.Amount),
MinAmount = g.Min(o => o.Amount)
})
.ToList();
In this example, we're using the GroupBy
method to group orders by the CustomerId
property. We're then using the Select
method to project the grouped data into a new anonymous type that contains the CustomerId
, the total amount of orders for each customer (TotalAmount
), the average amount of orders for each customer (AverageAmount
), the maximum amount of orders for each customer (MaxAmount
), and the minimum amount of orders for each customer (MinAmount
).
The ToList
method is used to execute the query and return the results as a list.