Path: redb/redb.Examples/Examples/E129_GroupByFiltered.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/E129_GroupByFiltered.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 Where filter applied before grouping.
/// Filter employees by salary, then group by department; one aggregate reads a field two nested
/// classes deep (HomeAddress.Building.Floor).
///
[ExampleMeta("E129", "GroupBy - With Filter", "Analytics",
ExampleTier.Free, 129, "GroupBy", "Where", "Filter", "Analytics", RelatedApis = ["IRedbQueryable.GroupBy", "IRedbQueryable.Where"])]
public class E129_GroupByFiltered : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Uncomment to see generated SQL:
//var sql = await redb.Query()
// .Where(x => x.Salary > 50000)
// .GroupBy(x => x.Department)
// .ToSqlStringAsync(g => new { Dept = g.Key, Count = Agg.Count(g) });
//Console.WriteLine(sql);
var byDeptFiltered = await redb.Query()
.Where(x => x.Salary > 50000m)
.GroupBy(x => x.Department)
.SelectAsync(g => new
{
Department = g.Key,
TotalSalary = Agg.Sum(g, x => x.Salary),
MaxFloor = Agg.Max(g, x => x.HomeAddress!.Building!.Floor),
Count = Agg.Count(g)
});
sw.Stop();
var first = byDeptFiltered.FirstOrDefault();
return Ok("E129", "GroupBy - With Filter", ExampleTier.Free, sw.ElapsedMilliseconds, byDeptFiltered.Count,
[$"Filter: Salary > 50k", $"Groups: {byDeptFiltered.Count}, First: {first?.Department ?? "N/A"}, Max home floor: {first?.MaxFloor}"]);
}
}