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); } }
public MenuNode AddNode(Menu menu) { MenuNode node = new MenuNode(menu); ChildNodes.Add(node); return node; }
private static void PrintTree(MenuNode m,int level) { try { for (int i = 0; i < level; i++) { Console.Write(" "); } level++; Console.WriteLine(m.MenuItem.Title); if (m.ChildNodes.Count == 0) { level--; return; } else { foreach (var n in m.ChildNodes) { PrintTree(n, level); } //level++; } } catch(System.Exception ex) { _exLogger.addErrorLog(ex); } }