I have the following data:
I need to get the top elements in the tree data structure. All the rows where the AncestorId
equals DescendantId
are parents but I need first to get the top parents. For example:
2 and 34
Becuase element with ID 2
just exist once in the descendantid
column, also for element with ID 34
.
Then I have to get the children below ID 2
and ID 34
and so on so I can start building my tree structure.
This is how the table data should look line in a visual tree:
Right now I have the following Linq statement to get the parents nodes:
// get parent nodes
var parentNodes = data.Where(i => i.AncestorId == i.DescedantId ).ToList();
How can I get in a linq query the Top parents, and then how to get the children, etc?
EDIT: To get the Top parents (i.e. they only appear once in Descendant Id), you can do the following
And to get the children of a parent: