【问题标题】:C# return a single list into nested JSON APIC# 将单个列表返回到嵌套的 JSON API
【发布时间】:2020-11-11 18:38:37
【问题描述】:

我有一个从我的数据库返回到我的 MVC API 的简单列表,示例查询是

select school, class, pupil from area

我正在添加到列表中

foreach (DataRow row in table.Rows)
{
    var area = new Area();
    area.school = row.ItemArray[0].ToString();
    area.class = row.ItemArray[1].ToString();
    area.pupil = row.ItemArray[2].ToString();
    returnedlist.Add(area);
}
 

此时 API 只是返回列表

{
  "School": "",
  "Class": "",
  "Pupil": ""
},
{
  "School": "",
  "Class": "",
  "Pupil": ""
},

但是,理想情况下,我希望它像下面这样嵌套返回

[
    {
        School: "Name",
        Class: [
            ClassName: '',
            Pupil: [
                    PupilName: ''},
                    PupilName: ''},
                    PupilName: '']

    },
    {
        School: "Name",
        Class: [
            ClassName: '',
            Pupil: [
                    PupilName: ''},
                    PupilName: ''},
                    PupilName: '']

    },

我可能已经扼杀了这一点,但你明白了。

数据的类还是很简单的

public class Area
{
    public string School { get; set; }
    public string Class { get; set; }
    public string Pupil { get; set; }
  
}

到目前为止,我已经尝试在列表等中返回一个列表,但没有任何运气。

非常感谢任何帮助。

【问题讨论】:

  • 可以分享area类和row吗,可以改变area类的结构。
  • 我已经按原样添加了类,很高兴改变结构,我尝试过但没有成功
  • 也许您可以更改 sql 查询或returnedlist。按学校和班级分组以构建新的预期对象列表。

标签: c# json api linq model-view-controller


【解决方案1】:

要获得预期的结果,您可以使用这种结构创建类:

1 - 类

public class NewArea
{
    public string School { get; set; }
    public List<Class> Classes { get; set; }
}

public class Class
{
    public string Name { get; set; }
    public List<string> Pupils { get; set; }
}

2 - 首先,分组 学校,对于每个分组项目,分组 班级

List<NewArea> newAreas = returnedlist
    .GroupBy(x => x.School)
    .Select(x => new NewArea
    {
        School = x.Key,
        Classes = x.GroupBy(y => y.Class).Select(z => new Class
        {
            Name = z.Key,
            Pupils = z.Select(w => w.Pupil).ToList()
        }).ToList()
    }).ToList();

3 - 测试示例:

List<Area> returnedlist = new List<Area>
{
    new Area{School = "s1", Class="c1",Pupil="p1"},
    new Area{School = "s1", Class="c1",Pupil="p2"},
    new Area{School = "s1", Class="c2",Pupil="p1"},
    new Area{School = "s1", Class="c2",Pupil="p2"},
    new Area{School = "s2", Class="c1",Pupil="p1"},
};

结果

[
   {
      "School":"s1",
      "Classes":[
         {
            "Name":"c1",
            "Pupils":[
               "p1",
               "p2"
            ]
         },
         {
            "Name":"c2",
            "Pupils":[
               "p1",
               "p2"
            ]
         }
      ]
   },
   {
      "School":"s2",
      "Classes":[
         {
            "Name":"c1",
            "Pupils":[
               "p1"
            ]
         }
      ]
   }
]

命名空间:

using System.Linq;
using System.Collections.Generic;

【讨论】:

  • 它抛出以下错误。 “NewArea”不包含“Class”的定义,并且找不到接受“NewArea”类型的第一个参数的可访问扩展方法“Class”(您是否缺少 using 指令或程序集引用?)
  • @MichaelCole 我不知道您是如何使用我的代码的,但它对我来说很好,请查看dotnetfiddle.net/Bel5i3。您是否在项目中添加了新类 NewAreaClass?。
猜你喜欢
  • 2015-02-03
  • 1970-01-01
  • 2020-01-31
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
相关资源
最近更新 更多