来自数据表的 Json 数据



在这里,我在转换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; }  

我从存储过程中收集了一个数据并推送到数据表中,这就是我使用 ConvertToDatatable() 的原因,那次我得到了一个包含 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;
}

这包含 3 个表数据。

所以..这是试图对数据进行分组,但这里得到的答案是类别顶部的类别在类别显示书籍类型,内部书籍类型显示类名列表,但我想要 3 组数据类别{},书籍类型{},类名{}

var result = rows.GroupBy(r => new { x = r["CategoryId"], y = r["CategoryName"] }).Select(g => new
{
    CategoryId = g.Key.x,
    CategoryName = g.Key.y,
    BookTypes = g.GroupBy(r => new { h = r["BookTypeId"], i = r["BookType"] }).Select(g1 => new
    {
        BookTypeId = g1.Key.h,
        BookType = g1.Key.i,
        ClassNames = g1.Select(r => new
        {
            ClassGroupId = r["ClassGroupId"],
            ClassName = r["ClassName"],
            ClassNumber = r["ClassNumber"]
        }),
    }),
});

     Rusult
This is my result
{    CategoryId:1   CategoryName:CD     ClassGroupId:15     ClassName:I     ClassNumber:1   BookTypeId:1    BookType:General CD}
{    CategoryId:2   CategoryName:DVD     ClassGroupId:16    ClassName:II    ClassNumber:2   BookTypeId:2    BookType:General DVD}
{    CategoryId:3   CategoryName:Book    ClassGroupId:17    ClassName:III   ClassNumber:3   BookTypeId:3    BookType:General Books}
 But i want the result like this   
+ Category={ CategoryId:1   CategoryName:CD     
    CategoryId:2    CategoryName:DVD    
    CategoryId:3    CategoryName:Book    }
   ClassGroup={ClassGroupId:15  ClassName:I     ClassNumber:1
    ClassGroupId:16     ClassName:II    ClassNumber:2
    ClassGroupId:17     ClassName:III   ClassNumber:3}
BookType{  BookTypeId:1 BookType:General CD
    BookTypeId:2    BookType:General DVD
    BookTypeId:3    BookType:General Books
}

在这里,我的结果是 Booktype 在 Category 下,类名在 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
            })
    };

小提琴:https://dotnetfiddle.net/h9qXqc

相关内容

  • 没有找到相关文章

最新更新