Path: redb/redb.Examples/Examples/E130_GroupByMultiKey.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Examples/Examples/E130_GroupByMultiKey.cs ---- using System.Diagnostics; using redb.Core; using redb.Core.Query.Aggregation; using redb.Examples.Models; using redb.Examples.Output; namespace redb.Examples.Examples; /// /// GroupBy with composite key (multiple fields). /// Group by Department AND Position for detailed breakdown; one aggregate reads a field two nested /// classes deep (HomeAddress.Building.Floor). /// [ExampleMeta("E130a", "GroupBy - Composite Key", "Analytics", ExampleTier.Free, 130, "GroupBy", "Composite", "MultiKey", "Analytics", RelatedApis = ["IRedbQueryable.GroupBy"])] public class E130_GroupByMultiKey : ExampleBase { public override async Task RunAsync(IRedbService redb) { var sw = Stopwatch.StartNew(); // Uncomment to see generated SQL: // var sql = await redb.Query() // .GroupBy(x => new { x.Department, x.Position }) // .ToSqlStringAsync(g => new { g.Key.Department, g.Key.Position, Count = Agg.Count(g) }); // Console.WriteLine(sql); var byDeptPosition = await redb.Query() .GroupBy(x => new { x.Department, x.Position }) .SelectAsync(g => new { g.Key.Department, g.Key.Position, TotalSalary = Agg.Sum(g, x => x.Salary), AvgFloor = Agg.Average(g, x => x.HomeAddress!.Building!.Floor), Count = Agg.Count(g) }); sw.Stop(); var first = byDeptPosition.FirstOrDefault(); return Ok("E130a", "GroupBy - Composite Key", ExampleTier.Free, sw.ElapsedMilliseconds, byDeptPosition.Count, [$"Groups: {byDeptPosition.Count}", $"First: {first?.Department ?? "N/A"}/{first?.Position ?? "N/A"}, Avg home floor: {first?.AvgFloor:F2}"]); } }