Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 数据表中的Json数据_Asp.net Mvc_Json.net - Fatal编程技术网

Asp.net mvc 数据表中的Json数据

Asp.net mvc 数据表中的Json数据,asp.net-mvc,json.net,Asp.net Mvc,Json.net,在这里,我对转换json的数据表有一个问题。这是我的课程,名为SearchCollection public int CategoryId { get; set; } public string CategoryName { get; set; } public int ClassGroupId { get; set; } public string ClassName { get; set; } public int ClassNumber { get; set; } public int B

在这里,我对转换json的数据表有一个问题。这是我的课程,名为
SearchCollection

public int CategoryId { get; set; }
public string CategoryName { get; set; }
public int ClassGroupId { get; set; }
public string ClassName { get; set; }
public int ClassNumber { get; set; }
public int BookTypeId { get; set; }
public string BookType { get; set; }  
我从存储过程中收集了一个数据,并将其推入数据表,这就是为什么我使用
converttodata()
,当时我得到了一个包含3个数据表的数据表

static DataTable ConvertToDatatable(IEnumerable<SearchCollection> list)    
{

    var dt = new DataTable();

    dt.Columns.Add("CategoryId");
    dt.Columns.Add("CategoryName");
    dt.Columns.Add("ClassGroupId");
    dt.Columns.Add("ClassName");
    dt.Columns.Add("ClassNumber");
    dt.Columns.Add("BookTypeId");
    dt.Columns.Add("BookType");

    foreach (var item in list)
    {
        var row = dt.NewRow();

        row["CategoryId"] = item.CategoryId;
        row["CategoryName"] = item.CategoryName;
        row["ClassGroupId"] = item.ClassGroupId;
        row["ClassName"] = item.ClassName;
        row["ClassNumber"] = item.ClassNumber;
        row["BookTypeId"] = item.BookTypeId;
        row["BookType"] = item.BookType;
        dt.Rows.Add(row);
    }
    return dt;
}

这里我的结果是booktype在category下,classname在booktype下。但是我想要的结果就像单个json中的3组记录一样,任何一个帮助就像单个json数据中的类别分组集合、类别分组集合和书籍类型集合一样。

这就是您想要的吗

    var result = new
    {
        Category = rows
            .GroupBy(r => new 
            { 
                x = r["CategoryId"],
                y = r["CategoryName"] 
            })
            .Select(g => new 
            { 
                CategoryId = g.Key.x,
                CategoryName = g.Key.y 
            }),
        ClassGroup = rows
            .GroupBy(r => new
            {
                x = r["ClassGroupId"],
                y = r["ClassName"],
                z = r["ClassNumber"]
            })
            .Select(g => new
            {
                ClassGroupId = g.Key.x,
                ClassName = g.Key.y,
                ClassNumber = g.Key.z
            }),
        BookType = rows
            .GroupBy(r => new
            {
                x = r["BookTypeId"],
                y = r["BookType"]
            })
            .Select(g => new
            {
                BookTypeId = g.Key.x,
                BookType = g.Key.y
            })
    };

Fiddle:

您是否可以编辑您的问题,以提供您在DataTable中拥有的数据以及您希望从该数据创建的JSON结果的示例?我不清楚你在问什么。我已经更新了我的问题。你能检查一下吗。。
    var result = new
    {
        Category = rows
            .GroupBy(r => new 
            { 
                x = r["CategoryId"],
                y = r["CategoryName"] 
            })
            .Select(g => new 
            { 
                CategoryId = g.Key.x,
                CategoryName = g.Key.y 
            }),
        ClassGroup = rows
            .GroupBy(r => new
            {
                x = r["ClassGroupId"],
                y = r["ClassName"],
                z = r["ClassNumber"]
            })
            .Select(g => new
            {
                ClassGroupId = g.Key.x,
                ClassName = g.Key.y,
                ClassNumber = g.Key.z
            }),
        BookType = rows
            .GroupBy(r => new
            {
                x = r["BookTypeId"],
                y = r["BookType"]
            })
            .Select(g => new
            {
                BookTypeId = g.Key.x,
                BookType = g.Key.y
            })
    };