Path: redb/redb.Examples/ExamplesEf/E106_TreeQueryChildrenOf_ef.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/ExamplesEf/E106_TreeQueryChildrenOf_ef.cs
----
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using redb.Examples.Ef;
using redb.Examples.Examples;
using redb.Examples.Output;
namespace redb.Examples.ExamplesEf;
/// EF Core twin of : the direct children of the CORP root. The root lookup is outside the measurement.
[ExampleMeta("E106_ef", "EF: Tree Query - WhereChildrenOf", "Trees", ExampleTier.Free, 1, "EF", "Tree")]
public class E106_TreeQueryChildrenOf_ef : EfExampleBase
{
public override async Task RunEfAsync(IDbContextFactory contexts)
{
await using var db = await contexts.CreateDbContextAsync();
var rootId = await db.Nodes().Where(d => d.Code == "CORP").OrderBy(d => d._id).Select(d => (long?)d._id).FirstOrDefaultAsync();
if (rootId == null)
return Fail("E106_ef", "EF: Tree Query - WhereChildrenOf", ExampleTier.Free, 0, "No tree. Run E088_ef first.");
var sw = Stopwatch.StartNew();
var children = (await db.Nodes().Where(d => d.objects._id_parent == rootId).ToListAsync())
.Select(DepartmentEfMapping.ToBusiness).ToList();
sw.Stop();
var names = children.Take(5).Select(c => c.Props.Code).ToArray();
return Ok("E106_ef", "EF: Tree Query - WhereChildrenOf", ExampleTier.Free, sw.ElapsedMilliseconds, children.Count,
[
$"Direct children of CORP: {children.Count}",
$"Codes: {string.Join(", ", names)}"
]);
}
}