Path: redb/redb.Examples/Examples/E072_AnyWithPredicate.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/E072_AnyWithPredicate.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Check if any employees match a condition using AnyAsync(predicate).
/// More efficient than Where().AnyAsync() - stops at first match.
///
[ExampleMeta("E072", "AnyAsync - With Predicate", "Query",
ExampleTier.Free, 72, "Any", "Predicate", "Filter", RelatedApis = ["IRedbQueryable.AnyAsync"])]
public class E072_AnyWithPredicate : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Check if any high earners exist (salary > 100k)
var query = redb.Query();
// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);
var anyHighEarners = await query.AnyAsync(e => e.Salary > 100_000m);
// Predicate over a field one nested class deep and a field two nested classes deep
var anyBerlinHighFloor = await query.AnyAsync(e => e.HomeAddress!.City == "Berlin" && e.HomeAddress!.Building!.Floor > 25);
sw.Stop();
return Ok("E072", "AnyAsync - With Predicate", ExampleTier.Free, sw.ElapsedMilliseconds,
anyHighEarners ? 1 : 0,
[$"Any with Salary > 100k: {anyHighEarners}", $"Any in Berlin above floor 25: {anyBerlinHighFloor}"]);
}
}