【发布时间】:2020-04-13 13:33:50
【问题描述】:
我们以这样的表格为例(数据必须在一个表格中):
我想得到这样的结果 JSON:
[
{
"manufacturer" : "VW",
"cars" : [
{
"id" : 1,
"model" : "Golf"
},
{
"id" : 3,
"model" : "Passat"
},
{
"id" : 6,
"model" : "Polo"
}
]
},
{
"manufacturer" : "Renault",
"cars" : [
{
"id" : 2,
"model" : "Laguna"
},
{
"id" : 5,
"model" : "Clio"
}
]
},
{
"manufacturer" : "Ford",
"cars" : [
{
"id" : 4,
"model" : "Fiesta"
}
}
]
问题是,当我尝试在不同的列表中获取制造商,然后使用 foreach 循环获取数据时,我收到有关数据读取器已打开的错误。
var manufacturers = _context.Cars.Select(s=> s.Manufacturer).Distinct();
foreach(var m in manufacturers)
{
var sublist = _context.Cars.Where(q=> String.Equals(q.Manufacturer, m).ToList();
// Add sublist to the specified models field (list)
}
我有一个模型,其中包含制造商字段和汽车列表。
【问题讨论】:
标签: c# linq lambda ef-core-3.0