Path: redb/redb.Examples/Examples/E094_TreeDescendants.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/E094_TreeDescendants.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Get all descendants of a node.
/// Returns all children, grandchildren, etc. recursively.
///
[ExampleMeta("E094", "Tree Descendants - All", "Trees",
ExampleTier.Free, 2, "Tree", "GetDescendantsAsync", "Recursive", "Pro")]
public class E094_TreeDescendants : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Find TechCorp root - use TreeQuery!
var roots = await redb.TreeQuery()
.Where(d => d.Code == "CORP")
.Take(1)
.ToListAsync();
if (roots.Count == 0)
{
return Ok("E094", "Tree Descendants - All", ExampleTier.Free, sw.ElapsedMilliseconds, 0,
["No tree found. Run E090 first."]);
}
var root = (TreeRedbObject)roots[0];
// Get ALL descendants (children, grandchildren, etc.)
var descendants = await redb.GetDescendantsAsync(root);
var list = descendants.ToList();
sw.Stop();
// Group by level
var byLevel = list.GroupBy(d => d.Level).OrderBy(g => g.Key).ToList();
var output = byLevel.Select(g => $"Level {g.Key}: {string.Join(", ", g.Select(d => d.Props.Code))}").ToList();
output.Insert(0, $"Root: {root.Name}, Total descendants: {list.Count}");
return Ok("E094", "Tree Descendants - All", ExampleTier.Free, sw.ElapsedMilliseconds, list.Count,
output.ToArray());
}
}