0

In my .NetCore3 project, I use HierarchyId to implement data tree structure. I am successful in storing as a tree, But I have trouble getting children.

MyModel :

    public int Id { get; set; }

    [Required]
    [StringLength(256)]
    public string Name { get; set; }

    public HierarchyId Level { get; set; }

My data is saved way:

And My Problem : My code is written for the return of children

var parentCategory = Context.HierarchyBOMs
            .AsQueryable()
            .FirstOrDefault(x => x.Id == id);

        var childCategoryQuery = Context.HierarchyBOMs
            .AsQueryable()
            .Where(x => x.Level.GetAncestor(1) == parentCategory.Level);

        var childList = childCategoryQuery.ToList();

But this code only returns one child.

How can all children be returned?

1 Answers1

1

I think you're looking for IsDescendantOf.

var childCategoryQuery = Context.HierarchyBOMs
    .Where(x => x.Level.IsDescendantOf(parentCategory.Level));
bricelam
  • 28,825
  • 9
  • 92
  • 117