Consider this hierarchy
A
|
--------------------------
| |
B C
| |
--------|-------- ---------
| | | | |
D E F G H
| | | |
| --------- ------- |
| | | | | |
I J K L M N
Every object has a Parent property and an Items collection for the choldren, so for instance, E has a parent of B, N of H, etc. A has a value of null for the parent. B.Items contains D-F, etc.
What is a LINQ statement that I can sort these by their level? I don't care about the sort order within a level (i.e. the order of D-H doesn't matter, but they have to come after B and C which have to come after A.
Only way I can think is two separate linq statements:
- Run an aggregate over this, calculating and storing the levels as you go
- Run a second LINQ query over the results ordering by Level.
B is easy of course. It's A that I'm struggling with. I can do it procedurally of course, but I have to think this can be reduced to a LINQ statement.