Path: redb/redb.Examples/Examples/E070_CountAsync.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/E070_CountAsync.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Count employees using CountAsync.
/// Get total count and filtered count.
///
[ExampleMeta("E070", "CountAsync - Total and Filtered", "Query",
ExampleTier.Free, 1, "Count", "Aggregate")]
public class E070_CountAsync : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Total count
var totalCount = await redb.Query().CountAsync();
// Filtered count
var managersCount = await redb.Query()
.Where(e => e.Position == "Manager")
.CountAsync();
// Filtered by a field two nested classes deep
var highFloorCount = await redb.Query()
.Where(e => e.HomeAddress!.Building!.Floor > 20)
.CountAsync();
sw.Stop();
return Ok("E070", "CountAsync - Total and Filtered", ExampleTier.Free, sw.ElapsedMilliseconds, totalCount,
[$"Total: {totalCount}, Managers: {managersCount}, Home floor above 20: {highFloorCount}"]);
}
}