I have a list of menu items in database, like:
ID ParentID ItemOrder Name
-------------------------------------------------------
1 null 1 Main_1
2 null 2 Main_2
3 null 3 Main_3
4 null 4 Main_4
5 null 5 Main_5
6 1 4 SubMenu_1.4
7 1 5 SubMenu_1.5
8 1 8 SubMenu_1.8
9 2 1 SubMenu_2.1
10 3 1 SubMenu_3.1
11 3 2 SubMenu_3.2
12 3 4 SubMenu_3.4
Submenu ItemOrder may not necessarily start at 1, it may begin with a 3, and the following menu item order can be 7 and so on.
How to create linq query or lambda expression that will return all parent (zero level) menu items and their first child (if exists)?
Expected result:
ID ParentID ItemOrder Name
-------------------------------------------------
1 null 1 Main_1
2 null 2 Main_2
3 null 3 Main_3
4 null 4 Main_4
5 null 5 Main_5
6 1 4 SubMenu_1.4
9 2 1 SubMenu_2.1
10 3 1 SubMenu_3.1
1 Answer