There is an object in the database type

class Node { public int Id public int ParentId public Node ParentNode public IEnumerable<Node> Childrens } 

It is necessary to get a flat list (array) of all children (and children of children, etc.) of the node.

  • But how to imagine a tree in a flat form? Give an example of the data and what you want to get - Bald
  • You need not a tree, but a list. In the base structure with Id and ParentId, you need to get the Id of all children from all levels of a node - Ssss
  • one
    so try it - Bald

1 answer 1

Try a yield recursion:

 static IEnumerable<Node> LoadDescendants(Node node) { foreach (Node child in node.Childrens) { yield return child; foreach (var childDescendant in LoadDescendants(child)) { yield return childDescendant; } } } 
  • And will it all pull up with one request or not? - Ssss
  • @sss probably not. EF does not know how to make hierarchical queries - in ORM it’s generally difficult (however, I can be wrong) - PashaPash