如何使用linq获取表中的datewise总数

时间:2014-07-16 07:15:54

标签: c# asp.net-mvc linq-to-sql

我希望将表格中的数据与总数明确一致 这是我的代码,我正在尝试做什么

var result = from a in db.table
                      group a by a.date.Date into g
                      select new { date = g.Key, distance = g.Sum(a => a.distance) };
 return View(result.ToList());

如果没有使用日期部分,那么我将获得一天中的所有数据。 但是通过使用这个linq我得到错误"指定的类型成员'日期' LINQ to Entities不支持。只有初始值设定项,实体成员和实体导航属性"。

1 个答案:

答案 0 :(得分:0)

试试这个,因为Date是类,实体框架不支持。你需要使用截断时间函数将日期时间转换为日期。

var result = from a in db.table
                      group a by EntityFunctions.TruncateTime(a.date) into g
                      select new { date = g.Key, distance = g.Sum(a => a.distance) };
 return View(result.ToList());

Entity Functions