MVC,JSON& amp; LINQ - 我创建了一个返回JSONResult的ActionResult:
var formhistory = from p in _formsRepository.ReturnedForms
where p.DateAdded >= DateTime.Now.Date.AddDays(-15) && p.DateAdded <= DateTime.Now.Date
group p by new {p.Centre, p.Form, p.DateAdded}
into g
select new {
g.Key.Centre,
g.Key.Form,
g.Key.DateAdded,
Total = g.Sum(p => p.Quantity)
};
return Json(formhistory, JsonRequestBehavior.AllowGet);
这给了我一个很好的JSON结果集如下:
[
{"Centre":"Centre1","Form":"Advice","DateAdded":"\/Date(1331856000000)\/","Total":1067},
{"Centre":"Centre1","Form":"Advice","DateAdded":"\/Date(1332460800000)\/","Total":808},
{"Centre":"Centre1","Form":"Advice","DateAdded":"\/Date(1333062000000)\/","Total":559},
{"Centre":"Centre1","Form":"Advice","DateAdded":"\/Date(1333666800000)\/","Total":1448}
]
我的问题是:我正在尝试操作这个JSON字符串,以便代替“Form”和“Total”的2个键/值对,我只有1,即“Form”:“Total”。
我意识到这可能是一个非常基本的问题,但有人能指出我正确的方向吗? (除了门!)
答案 0 :(得分:2)
select new {
g.Key.Centre,
//g.Key.Form,
g.Key.DateAdded,
Form = g.Sum(p => p.Quantity)
}
会给你一个键“Form”,其值为前者“Total”。这就是你想要的吗?
答案 1 :(得分:0)
修改linq查询中的选择部分
select new {
g.Key.Centre,
g.Key.DateAdded,
NewField = String.Format("{0} - {1}",g.Key.Form,g.Sum(p => p.Quantity).ToString())
};
我认为这会解决你的目的。