❔ How to get all children of parent node?
I have a DTO as image. I want to get all children (included children of child) . How can I achive this ? Thanks

dbContext.Nodes.Where(node => node.ParentId == id){
"data": [
{
"id": 2,
"name": "General",
"parentId": "0"
},
{
"id": 3,
"name": "Fruits",
"parentId": "2"
},
{
"id": 6,
"name": "Vegetables",
"parentId": "2"
},
{
"id": 12,
"name": "Apple",
"parentId": "3"
}
],
"success": true,
"messages": null
}/close dbContext.Nodes.Where(node => node.ParentId == id){
"data": [
{
"id": 2,
"name": "General",
"parentId": "0"
},
{
"id": 3,
"name": "Fruits",
"parentId": "2"
},
{
"id": 6,
"name": "Vegetables",
"parentId": "2"
},
{
"id": 12,
"name": "Apple",
"parentId": "3"
}
],
"success": true,
"messages": null
}IEnumerable<Node> GetDescendants(Node node)
{
var children = dbContext.Nodes.Where(n => n.ParentId == node.Id);
return children.Concat(children.SelectMany(GetDescendants));
}internal static class GenericHelpers
{
public static IEnumerable<TreeItem<T>> GenerateTree<T, K>(
this IEnumerable<T> collection,
Func<T, K> idSelector,
Func<T, K> parentIdSelector,
K rootId = default( K ) )
{
foreach ( var c in collection.Where( c => EqualityComparer<K>.Default.Equals( parentIdSelector( c ), rootId ) ) )
{
yield return new TreeItem<T>
{
Item = c,
Children = collection.GenerateTree( idSelector, parentIdSelector, idSelector( c ) )
};
}
}
}
var root = yourCollection.GenerateTree(
item=> item.Id,
item=> item.ParentId );