Consider the following rows in a database:
Id | Parent
__________________
1 null
2 1
3 2
4 3
5 null
6 5
Each Id that has a null Parent is the “Owner”/”Super Parent”.
What would be the best approach, performance wise, to collect the parents and their children ? Should I use LINQ or Stored Procedures ?
I want the end result to be IEnumerable<IEnumerable<int>>.
In SQL, you could query using a CTE. For example, to retrieve a list of nodes with their parent and the highest parent in their tree:
This gives:
Note that I changed your example data so row 2 is no longer a child of itself, but a child of row 1.