0

I have a class with following structure

public class Menu
{
    public Menu()
    {
        ChildMenus = new List<Menu>();
    }

    public string MenuName { get; set; }
    public int Id { get; set; }
    public int Level { get; set; }
    public int Order { get; set; }
    public string RefCode { get; set; }
    public List<Menu> ChildMenus { get; set; }
    public int ParentId { get; set; }
}

And collection of menu objects like:

public class MenuManager
{
    public List<Menu> Menucollection;

    public MenuManager()
    {
        Menucollection = new List<Menu>
        {
            new Menu {MenuName = "Products", Id = 1, ParentId = 0, Level = 1, RefCode = "0", Order = 1},
            new Menu {MenuName = "Sales", Id = 2, ParentId = 0, Level = 1, RefCode = "0", Order = 2},
            new Menu {MenuName = "Contacts", Id = 3, ParentId = 0, Level = 1, RefCode = "0", Order = 3},
            new Menu {MenuName = "Mobiles", Id = 4, ParentId = 1, Level = 2, RefCode = "0", Order = 1},
            new Menu {MenuName = "Computers", Id = 5, ParentId = 1, Level = 2, RefCode = "0", Order = 2},
            new Menu {MenuName = "Local Sales", Id = 6, ParentId = 2, Level = 2, RefCode = "0", Order = 1},
            new Menu {MenuName = "Forgin Sales", Id = 7, ParentId = 2, Level = 2, RefCode = "0", Order = 2},
            new Menu {MenuName = "Email", Id = 8, ParentId = 3, Level = 2, RefCode = "100", Order = 1},
            new Menu {MenuName = "Nokia", Id = 9, ParentId = 4, Level = 3, RefCode = "110", Order = 1},
            new Menu {MenuName = "LG", Id = 10, ParentId = 4, Level = 3, RefCode = "111", Order = 2},
            new Menu {MenuName = "Accer", Id = 11, ParentId = 5, Level = 3, RefCode = "210", Order = 1},
            new Menu {MenuName = "Kerala", Id = 9, ParentId = 6, Level = 3, RefCode = "110", Order = 1}
        };
    }

    public void BuildMenu()
    {
    }
}

I need to build a menu structure from the collection, with following condition:

Must drop the menu item if:

  1. If it has no any child.
  2. If its refcode equals 0.

So the expected result based on the input should be:

Product    Sales         Contact
Mobile     Local Sales   Email
Nokia      Kerala
LG
Computer 
Accer                
Ocelot20
  • 10,510
  • 11
  • 55
  • 96
Binson Eldhose
  • 993
  • 3
  • 14
  • 35

1 Answers1

1

First, contrary to a previous comment recursion is not required to deal with trees. It is often the simplest, easiest method (and, incidentally, what I provided) - but not required (and there are sometimes performance reasons for avoiding recursion).

Note, the extension method is based off a snipped I stole from here: Searching a tree using LINQ

            var filteredMenus = from m in Menucollection
                                where m.RefCode != "0" || Menucollection.Any(m1=>m1.ParentId == m.Id)
                                select m;

            foreach(var m in filteredMenus)
            {
                m.ChildMenus = filteredMenus.Where(m1=>m1.ParentId == m.Id).ToList();
            }

            var expanded = from m in filteredMenus
                       where m.ParentId == 0
                       select new { Menu = m, Descendants = m.AsBreadthFirstEnumerable(m1 => m1.ChildMenus) };

            foreach (var m in expanded)
            {
                System.Diagnostics.Debug.WriteLine("Menu: " + m.Menu.MenuName);
                foreach (var m1 in m.Descendants)
                {
                    System.Diagnostics.Debug.WriteLine("- " + m1.MenuName);
                }
            }

And then in some static class:

    public static IEnumerable<T> AsBreadthFirstEnumerable<T>(this T head, Func<T, IEnumerable<T>> childrenFunc)
    {
        yield return head;

        var last = head;
        foreach (var node in AsBreadthFirstEnumerable(head, childrenFunc))
        {
            foreach (var child in childrenFunc(node))
            {
                yield return child;
                last = child;
            }
            if (last.Equals(node)) yield break;
        }
    }

It's not purely LINQ - but it should give you something from which to start.

Community
  • 1
  • 1
Mike
  • 643
  • 3
  • 10