我尝试在LINQ中重新创建以下SQL查询以返回JSON:
SELECT a.Title, COUNT(b.ActionID) FROM labels a
FULL JOIN [Values] b ON a.ID = b.ID
GROUP BY a.Title
我试过了:
public ActionResult Json()
{
IEnumerable < object > query = (from a in db.Labels
join b in db.Values on a.ID equals b.ID
group b.ID by a.Title into grp
select new
{
Label = grp.Key,
Value = grp.Count()
}).ToList();
return Json(query, JsonRequestBehavior.AllowGet);
}
结果仅返回具有值的标签:
[{"Label":"New","Value":2}]
但我需要的是:
[
{"Label":"A","Value":2},
{"Label":"B","Value":0},
{"Label":"C","Value":0},
]
答案 0 :(得分:2)
你为什么不这样做:
IEnumerable < object > query = (from a in db.Labels
select new
{
Label = a.Title,
Value = db.Values
.Where(w=>w.ID==a.ID)
.Count()
}).ToList();