C#
Beginner
1 min read
GroupBy and Aggregation
Example
// GroupBy, aggregation, and ToLookup
record Sale(string Region, string Product, decimal Amount, DateTime Date);
var sales = new List<Sale>
{
new("North", "Widget", 120m, new DateTime(2024, 1, 5)),
new("South", "Gadget", 250m, new DateTime(2024, 1, 7)),
new("North", "Gadget", 180m, new DateTime(2024, 2, 3)),
new("East", "Widget", 90m, new DateTime(2024, 2, 14)),
new("South", "Widget", 310m, new DateTime(2024, 3, 1)),
new("North", "Widget", 200m, new DateTime(2024, 3, 18)),
};
// GroupBy region with aggregation
var byRegion = sales
.GroupBy(s => s.Region)
.Select(g => new
{
Region = g.Key,
Total = g.Sum(s => s.Amount),
Count = g.Count(),
Avg = g.Average(s => s.Amount)
})
.OrderByDescending(r => r.Total);
foreach (var r in byRegion)
Console.WriteLine($"{r.Region,-8} Total={r.Total:C} Avg={r.Avg:C} Count={r.Count}");
// Multi-level grouping — by region then product
var detailed = sales
.GroupBy(s => s.Region)
.ToDictionary(
g => g.Key,
g => g.GroupBy(s => s.Product)
.ToDictionary(pg => pg.Key, pg => pg.Sum(s => s.Amount)));
foreach (var (region, products) in detailed)
foreach (var (product, total) in products)
Console.WriteLine($" {region}/{product}: {total:C}");
// ToLookup — allows multiple values per key (immutable after creation)
var lookup = sales.ToLookup(s => s.Product);
foreach (Sale northSale in lookup["Widget"])
Console.WriteLine($"Widget sale: {northSale.Region} {northSale.Amount:C}");