Path: redb/redb.Examples/Examples/E126_MinMaxAsync.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/E126_MinMaxAsync.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Calculates min and max of numeric fields using MinAsync/MaxAsync.
/// Server-side aggregation without loading objects, over a field of the employee and over a field
/// two nested classes deep (HomeAddress.Building.Floor).
///
[ExampleMeta("E126", "MinAsync/MaxAsync - Range", "Analytics",
ExampleTier.Free, 126, "Min", "Max", "Aggregation", "Analytics", RelatedApis = ["IRedbQueryable.MinAsync", "IRedbQueryable.MaxAsync"])]
public class E126_MinMaxAsync : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
var query = redb.Query();
// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);
var minSalary = await query.MinAsync(e => e.Salary);
var maxSalary = await query.MaxAsync(e => e.Salary);
var minFloor = await query.MinAsync(e => e.HomeAddress!.Building!.Floor);
var maxFloor = await query.MaxAsync(e => e.HomeAddress!.Building!.Floor);
sw.Stop();
return Ok("E126", "MinAsync/MaxAsync - Range", ExampleTier.Free, sw.ElapsedMilliseconds, 4,
[$"MIN(Salary) = {minSalary:N0}", $"MAX(Salary) = {maxSalary:N0}",
$"MIN/MAX(HomeAddress.Building.Floor) = {minFloor}..{maxFloor}"]);
}
}