Path: redb/redb.Examples/Examples/E195_GroupByWindowFiltered.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/E195_GroupByWindowFiltered.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 + Window with Where filter applied before grouping.
/// Filter employees by salary, group by department, then rank groups.
/// Tests Pro filter handling in GroupedWindow queries.
///
[ExampleMeta("E195", "GroupBy + Window - With Filter", "Analytics",
ExampleTier.Pro, 195, "GroupBy", "Window", "Where", "Filter", "Rank", RelatedApis = ["IRedbGroupedQueryable.WithWindow", "IRedbQueryable.Where"])]
public class E195_GroupByWindowFiltered : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Filter high earners, group by department, rank by total salary
var ranked = await redb.Query()
.Where(x => x.Salary > 60000m)
.GroupBy(x => x.Department)
.WithWindow(w => w.OrderByDesc(g => Agg.Sum(g, x => x.Salary)))
.SelectAsync(g => new
{
Department = g.Key,
TotalSalary = Agg.Sum(g, x => x.Salary),
Count = Agg.Count(g),
Rank = Win.Rank()
});
//var sql = await redb.Query()
// .Where(x => x.Salary > 60000m)
// .GroupBy(x => x.Department)
// .WithWindow(w => w.OrderByDesc(g => Agg.Sum(g, x => x.Salary)))
// .ToSqlStringAsync(g => new
// {
// Department = g.Key,
// TotalSalary = Agg.Sum(g, x => x.Salary),
// Count = Agg.Count(g),
// Rank = Win.Rank()
// });
//Console.WriteLine(sql);
sw.Stop();
var top = ranked.FirstOrDefault();
return Ok("E195", "GroupBy + Window - With Filter", ExampleTier.Pro, sw.ElapsedMilliseconds, ranked.Count,
[
$"Filter: Salary > 60k",
$"Groups: {ranked.Count}",
$"#1: {top?.Department ?? "N/A"} (Total: {top?.TotalSalary:N0})"
]);
}
}