I have a hierarchial data table as follows which generates menu and its sub menus. main menu has parentId as 0. Submenu has parent Ids referring to parentId.
ResourceId DisplayName ParentId Url
-----------------------------------------------
1 Home 0 Some Url
2 Student 0 Some Url
3 Staff 0 Some Url
4 Library 0 Some Url
6 StudentAtt 1 Some Url
7 TimeTable 1 Some Url
8 Staff Att 2 Some Url
9 Book Issue 3 Some Url
10 Book Return 3 Some Url
11 Fee Payment 4 Some Url
12 Book fine 10 Some Url
need to convert it to Json. Below is the code i tried out. I am trying to check if ParentId of SubMenu equals ResourceId of main Menu. But subMenu is not displayed. (variable table is DataTable)
var rows = table.Rows.Cast<DataRow>().ToList();
var result = rows.Where(x => x["ParentId"].ToString() == "0").GroupBy(r => new { x = r["ResourceId"] }).Select(
g => new
{
//MenuLevel = g.Key.x,
MenuDetails = g.GroupBy(r => new {a = r["DisplayName"], b = r["Url"]}).Select(
detail => new
{
DisplayName = detail.Key.a,
Url = detail.Key.b,
SubMenu =
detail.Where(y => g.Key.x.ToString()==y["ParentId"].ToString()).
GroupBy(r => new {f = r["DisplayName"]}).Select(
subMenu=>new
{
SubMenuDisplayName=subMenu.Key.f
}
)
}
)
});
the result i got is as below.
[{"MenuDetails":[{"DisplayName":"Home","Url":null,"SubMenu":[]}]},{"MenuDetails":[{"DisplayName":"Student","Url":null,"SubMenu":[]}]},{"MenuDetails":[{"DisplayName":"Staff","Url":null,"SubMenu":[]}]},{"MenuDetails":[{"DisplayName":"Library","Url":null,"SubMenu":[]}]}]
But expected result is:
[{"MenuDetails":[{"DisplayName":"Home","Url":null,"SubMenu":[{"SubMenuDisplayName":"StudentAtt"},{"SubMenuDisplayName":"TimeTable"}]}]},{"MenuDetails":[{"DisplayName":"Student","Url":null,"SubMenu":[{"SubMenuDisplayName":"Staff Att"}]}]},{"MenuDetails":[{"DisplayName":"Staff","Url":null,"SubMenu":[{"SubMenuDisplayName":"Book Issue"},{"SubMenuDisplayName":"Book Return"}]}]},{"MenuDetails":[{"DisplayName":"Library","Url":null,"SubMenu":[{"SubMenuDisplayName":"Fee Payment "}]}]}]
I also need to display the sub sub menu (which has parent id pointing to resource id of sub menu) Any one please help