Path: redb/redb.Examples/Examples/E097_TreeQueryRoots.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/E097_TreeQueryRoots.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Find root nodes in tree.
/// WhereRoots returns only top-level nodes (parent_id = null).
///
[ExampleMeta("E097", "Tree Query - Roots Only", "Trees",
ExampleTier.Free, 2, "Tree", "TreeQuery", "WhereRoots", "Pro",
RelatedApis = ["ITreeQueryable.WhereRoots", "IRedbService.TreeQuery"])]
public class E097_TreeQueryRoots : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Find all root nodes
var roots = await redb.TreeQuery()
.WhereRoots()
.ToListAsync();
sw.Stop();
var names = roots.Select(r => $"{r.Name} ({r.Props.Code})").ToArray();
return Ok("E097", "Tree Query - Roots Only", ExampleTier.Free, sw.ElapsedMilliseconds, roots.Count,
[
"Filter: WhereRoots() - parent_id IS NULL",
$"Found: {string.Join(", ", names)}"
]);
}
}