static void Main(string[] args) { _exLogger = new ExceptionLogger(); try { menuCollection = new List<Menu>(); InitializeData(menuCollection); var roots = from menu in menuCollection where menu.ParentId == null select menu; Menu m = new Menu { Id = 0, Title = "Main" }; MenuNode rootNode = new MenuNode(m); foreach (var r in roots) { Console.WriteLine(); MenuNode node = rootNode.AddNode(r); node.MenuItem = r; PopulateTree(node); } Console.WriteLine("---------------------------------------------------------------------"); PrintTree(rootNode, 0); Console.ReadKey(); } catch(System.Exception ex) { _exLogger.addErrorLog(ex); } }
static void PopulateTree(MenuNode menuNode) { try { var children = from menu in menuCollection where menu.ParentId == menuNode.MenuItem.Id select menu; if (children.Count() == 0) { return; } else { foreach (var c in children) { MenuNode newNode = menuNode.AddNode(c); PopulateTree(newNode); } } } catch(System.Exception ex) { _exLogger.addErrorLog(ex); } }