1

一个非常新的 MVC、JSON 和 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”。

我意识到这可能是一个非常基本的问题,但是谁能指出我正确的方向?(除了门!)

4

2 回答 2

2
select new {
   g.Key.Centre,
   //g.Key.Form,
   g.Key.DateAdded,
   Form = g.Sum(p => p.Quantity)
}

会给你一个关键的“表格”,它的价值是以前的“总计”。那是你要的吗?

于 2012-04-27T10:00:55.823 回答
0

修改 linq 查询中的选择部分

   select new {  
      g.Key.Centre, 
      g.Key.DateAdded,  
      NewField =  String.Format("{0} - {1}",g.Key.Form,g.Sum(p => p.Quantity).ToString())                                     
     }; 

我认为这将解决您的目的。

于 2012-04-27T10:08:45.693 回答