Path: redb/redb.Examples/Examples/E102_TreeIsDescendant.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/E102_TreeIsDescendant.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Extensions;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Check if node is descendant of another.
/// IsDescendantOfAsync - optimized check without loading full tree.
///
[ExampleMeta("E102", "Tree - IsDescendantOf", "Trees",
ExampleTier.Free, 2, "Tree", "IsDescendantOfAsync", "Pro", Order = 102)]
public class E102_TreeIsDescendant : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
// Find root and a deep node
var roots = await redb.TreeQuery()
.Where(d => d.Code == "CORP")
.Take(1)
.ToListAsync();
var teams = await redb.TreeQuery()
.Where(d => d.Code.StartsWith("TEAM-"))
.Take(1)
.ToListAsync();
if (roots.Count == 0 || teams.Count == 0)
return Fail("E102", "Tree - IsDescendantOf", ExampleTier.Free, 0, "No tree. Run E088 first.");
var root = roots[0];
var team = teams[0];
// Check if team is descendant of root
var sw = Stopwatch.StartNew();
var isDescendant = await team.IsDescendantOfAsync(root, redb);
var isNotDescendant = await root.IsDescendantOfAsync(team, redb);
sw.Stop();
return Ok("E102", "Tree - IsDescendantOf", ExampleTier.Free, sw.ElapsedMilliseconds, 2,
[
$"{team.Props.Code} descendant of {root.Props.Code}: {isDescendant}",
$"{root.Props.Code} descendant of {team.Props.Code}: {isNotDescendant}"
]);
}
}