using System.Collections.Generic;
private static readonly IList<Menu> MenuItems = new List<Menu>
new Menu(1, -1, "Menu1"),
new Menu(2, -1, "Menu2"),
new Menu(3, -1, "Menu3"),
public Menu(int id, int parentId, string name)
public int ParentId { get; }
public string Name { get; }
public static void Main()
Console.WriteLine("Printed menu:");
private static void PrintMenu(IEnumerable<Menu> menuItems)
foreach (var menu in menuItems)
var parentItems = menuItems.Where(x => x.Id == menu.ParentId);
var childItems = MenuItems.Where(x => x.ParentId == menu.Id);
if (parentItems.Count() == 0)
Console.WriteLine(menu.Name);
if (childItems.Count() > 0)
if (parentItems.Count() > 0)