如何将分层数据从DataTable转换为JSON

时间:2015-02-16 03:59:24

标签: .net json asp.net-mvc linq json.net

我有一个分层数据表,如下所示,它生成一个菜单及其子菜单。主菜单的ParentId0。每个子菜单都有ParentId引用表格中另一行的ResourceId

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

我需要将数据转换为JSON。下面是我试过的代码。我正在尝试检查子菜单的ParentId是否等于主菜单的ResourceId。但是不显示子菜单。 (变量tableDataTable。)

    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
                        })
                })
        });

我得到的结果如下:

[
    {
        "MenuDetails": [
            {
                "DisplayName": "Home",
                "Url": null,
                "SubMenu": []
            }
        ]
    },
    {
        "MenuDetails": [
            {
                "DisplayName": "Student",
                "Url": null,
                "SubMenu": []
            }
        ]
    },
    {
        "MenuDetails": [
            {
                "DisplayName": "Staff",
                "Url": null,
                "SubMenu": []
            }
        ]
    },
    {
        "MenuDetails": [
            {
                "DisplayName": "Library",
                "Url": null,
                "SubMenu": []
            }
        ]
    }
]

但预期的结果是:

[
    {
        "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 "
                    }
                ]
            }
        ]
    }
]

我还需要显示子子菜单(其ParentId指向子菜单的ResourceId。)

0 个答案:

没有答案