Rebuild typed trees from flat results
SQL describes what the database executes. From and Join separately describe
how columns are split and how child rows are attached to their parents.
One result, several independent branches
Each instructor root has assigned courses, an optional office, and managed departments with their own courses. Keys deduplicate every repeated record produced by the independent joins.
InstructorDataNodes.cs
await using var fusionConnection = await context.OpenFusionConnectionAsync();
const string sql =
"""
SELECT
P.ID, P.LastName, P.FirstMidName, P.EnrollmentDate, P.Discriminator,
CA.InstructorID, CA.CourseID,
C.CourseID, C.Title, C.Credits, C.DepartmentID,
D.DepartmentID, D.Name, D.Budget, D.InstructorID,
O.InstructorID, O.Location,
D1.DepartmentID, D1.Name, D1.Budget, D1.InstructorID,
C2.CourseID, C2.Title, C2.Credits, C2.DepartmentID
FROM Person P
LEFT JOIN CourseAssignment CA ON P.ID = CA.InstructorID
LEFT JOIN Course C ON CA.CourseID = C.CourseID
LEFT JOIN Department D ON C.DepartmentID = D.DepartmentID
LEFT JOIN OfficeAssignment O ON P.ID = O.InstructorID
LEFT JOIN Department D1 ON P.ID = D1.InstructorID
LEFT JOIN Course C2 ON D1.DepartmentID = C2.DepartmentID
WHERE P.Discriminator = 'Instructor'
ORDER BY P.ID, CA.CourseID, D1.DepartmentID, C2.CourseID;
""";
// The shape mirrors the record blocks returned by the LEFT JOINs above.
// It interprets the result; it does not generate or execute the joins.
var shape = new From<Person>(
new Join<CourseAssignment>(
new Join<Course>(
new Join<Department>())),
new Join<OfficeAssignment>(),
new Join<Department>(
new Join<Course>()));
var dataNodes = await fusionConnection.GetDataNodesAsync(sql, shape);
return dataNodes;
Navigate the result
Each typed node exposes its value through Data. Navigation helpers select direct
children or recursively enumerate descendants without introducing navigation properties on the model.
NavigateDataNodes.cs
await using var fusionConnection = await context.OpenFusionConnectionAsync();
var instructors = await fusionConnection.GetDataNodesAsync(
"""
SELECT
P.ID, P.LastName, P.FirstMidName, P.EnrollmentDate, P.Discriminator,
CA.InstructorID, CA.CourseID,
C.CourseID, C.Title, C.Credits, C.DepartmentID,
D.DepartmentID, D.Name, D.Budget, D.InstructorID,
O.InstructorID, O.Location,
D1.DepartmentID, D1.Name, D1.Budget, D1.InstructorID,
C2.CourseID, C2.Title, C2.Credits, C2.DepartmentID
FROM Person P
LEFT JOIN CourseAssignment CA ON P.ID = CA.InstructorID
LEFT JOIN Course C ON CA.CourseID = C.CourseID
LEFT JOIN Department D ON C.DepartmentID = D.DepartmentID
LEFT JOIN OfficeAssignment O ON P.ID = O.InstructorID
LEFT JOIN Department D1 ON P.ID = D1.InstructorID
LEFT JOIN Course C2 ON D1.DepartmentID = C2.DepartmentID
WHERE P.Discriminator = 'Instructor'
ORDER BY P.ID, CA.CourseID, D1.DepartmentID, C2.CourseID;
""",
new From<Person>(
new Join<CourseAssignment>(
new Join<Course>(
new Join<Department>())),
new Join<OfficeAssignment>(),
new Join<Department>(
new Join<Course>())));
var instructor = instructors.Single(node => node.Data.ID == 1);
var assignment = instructor.Children<CourseAssignment>().First();
var course = assignment.SingleChild<Course>();
var courseDepartment = course.SingleChild<Department>();
var office = instructor.SingleOrDefaultChild<OfficeAssignment>();
var managedDepartments = instructor.Children<Department>();
return new
{
Instructor = $"{instructor.Data.FirstMidName} {instructor.Data.LastName}",
AssignedCourses = instructor.Children<CourseAssignment>().Count,
FirstAssignedCourse = course.Data.Title,
CourseDepartment = courseDepartment.Data.Name,
Office = office?.Data.Location,
ManagedDepartments = managedDepartments.Count,
ManagedCourses = managedDepartments.Sum(node => node.Children<Course>().Count),
AllCoursesBelowInstructor = instructor.Descendants<Course>().Count()
};