C# Asp.net mvc中的嵌套Groupby函数
我有这个函数将JSON数据返回到我的视图C# Asp.net mvc中的嵌套Groupby函数,c#,json,group-by,asp.net-mvc-5,C#,Json,Group By,Asp.net Mvc 5,我有这个函数将JSON数据返回到我的视图 public JsonResult MeterReading() { db.Configuration.ProxyCreationEnabled = false; var queryNestedGroups = from s in db.Sales group s by s.PointId into newGroup1 from newGroup2 in (from st
public JsonResult MeterReading()
{
db.Configuration.ProxyCreationEnabled = false;
var queryNestedGroups =
from s in db.Sales
group s by s.PointId into newGroup1
from newGroup2 in
(from student in newGroup1
group student by student.FuelTypeId into g
select new
{
MeterReading = g.Sum(o => o.SaleQuantity),
FuelType = db.zFuelTypes.Where(u => u.Id == g.Key).Select(p => p.FuelType).FirstOrDefault(),
Point = db.DispensePoints.Where(u => u.Id == newGroup1.Key).Select(p => p.PointName).FirstOrDefault(),
Picture=db.DispensePoints.Where(u => u.Id == newGroup1.Key).Select(p => p.Picture).FirstOrDefault()
})
group newGroup2 by newGroup1.Key;
return Json(queryNestedGroups, JsonRequestBehavior.AllowGet);
}
但它显示了这样的数据
[[{"MeterReading":677.00,"FuelType":"Petrol","Point":"wwwfhfghfgh","Picture":"~/Images/PointPicuters/637321819464806362a.png"},{"MeterReading":677.00,"FuelType":"GasLocal","Point":"wwwfhfghfgh","Picture":"~/Images/PointPicuters/637321819464806362a.png"}],[{"MeterReading":677.00,"FuelType":"Petrol_95","Point":"www","Picture":"~/Images/PointPicuters/63732163747332.jpg"}}]]
但我想要这种格式
[{"Point":"wwwfhfghfgh","Picture":"~/Images/PointPicuters/637321819464806362a.png",["MeterReading":677.00,"FuelType":"Petrol"],["MeterReading":677.00,"FuelType":"GasLocal"]},{"MeterReading":677.00,"FuelType":"Petrol_95",["Point":"www","Picture":"~/Images/PointPicuters/63732163747332.jpg"]}]
谢谢我假设您的
db.Sales
数据如下:
List mySales=new List()
{
新销售
{
Id=1,
PointName=“wwwfhfghfgh”,
Picture=“~/Images/PointPicuters/637321819464806362a.png”,
FullType=“汽油”,
销售数量=100.00米
},
新销售
{
Id=2,
PointName=“wwwfhfghfgh”,
Picture=“~/Images/PointPicuters/637321819464806362a.png”,
FullType=“汽油”,
销售数量=200.00米
},
新销售
{
Id=3,
PointName=“wwwfhfghfgh”,
Picture=“~/Images/PointPicuters/637321819464806362a.png”,
FullType=“GasLocal”,
销售数量=300.00米
},
新销售
{
Id=4,
PointName=“wwwfhfghfgh”,
Picture=“~/Images/PointPicuters/637321819464806362a.png”,
FullType=“GasLocal”,
销售数量=400.00米
},
新销售
{
Id=5,
PointName=“wwwfhfghfgh”,
Picture=“~/Images/PointPicuters/637321819464806362a.png”,
FullType=“汽油机”,
销售数量=500.00米
},
新销售
{
Id=6,
PointName=“wwwfhfghfgh”,
Picture=“~/Images/PointPicuters/637321819464806362a.png”,
FullType=“汽油机”,
销售数量=600.00米
},
新销售
{
Id=7,
PointName=“www”,
Picture=“~/Images/PointPicuters/6373216374732.jpg”,
FullType=“MyNewType”,
销售数量=10000.00米
},
新销售
{
Id=8,
PointName=“www”,
Picture=“~/Images/PointPicuters/6373216374732.jpg”,
FullType=“MyNewType”,
销售数量=20000.00米
}
};
术后2个月按手术分组
var queryGroupByFullType=来自mySales中的s
按s.FullType将s分组到新组1中
选择新的
{
MeterReading=newGroup1.Sum(x=>x.SaleQuantity),
PointName=newGroup1.First().PointName,
Picture=newGroup1.First().Picture,
FullType=newGroup1.Key
};
变量queryGroypByPointName=来自queryGroupByFullType中的s
按s.PointName将s分组到新组1中
选择新的
{
Point=newGroup1.Key,
Picture=newGroup1.First().Picture,
MyList=newGroup1。选择(x=>new
{
x、 计量读数,
x、 全型
})
};
json:
[
{
"point":"wwwfhfghfgh",
"picture":"~/Images/PointPicuters/637321819464806362a.png",
"myList":[
{
"meterReading":300.00,
"fullType":"Petrol"
},
{
"meterReading":700.00,
"fullType":"GasLocal"
},
{
"meterReading":1100.00,
"fullType":"Petrol_95"
}
]
},
{
"point":"www",
"picture":"~/Images/PointPicuters/63732163747332.jpg",
"myList":[
{
"meterReading":30000.00,
"fullType":"MyNewType"
}
]
}
]
所需的JSON无效。在JSON中,键值对不能直接位于数组内部(由
[]
表示);它们必须位于对象中(由{}
表示)。有关正确JSON构造的信息,请参阅。您可以使用来验证JSON。请编辑您的问题以包含正确的JSON。谢谢亲爱的,您救了我的命。@mustafateb如果这对您有帮助,请接受我最好的答案,谢谢~我从您的答案中了解到,当我将此用于您给定的列表时,它工作正常,但当我将其用于数据库表时,它只返回一条记录而不是全部,我不知道为什么