Whenever you have a parent child relationship and you are not sure how may levels deep it is structured you need to think about recursion.
I’m going to provide a full solution to take a flat normalized collection with a parent/child relationship and use LINQ with recursion to transform into nested collection.
Below is the source for this sample. If you create a new Console Application named RecursionTest and paste this code in the Program.cs file it will run and display the output.
internal class Program
private static void Main(string args)
// Fill list with sample data
var flatObjects = new List
bsp; DumpTreeRootNode(childNode, ++level);