如何使用linq获取表中的日期合计

本文关键字:日期 何使用 linq 获取 | 更新日期: 2023-09-27 18:13:18

我想获得数据在我的表与total日期明智这是我的代码我想做什么

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到实体。

如何使用linq获取表中的日期合计

试试这个,因为Date是类,不被实体框架支持。你需要使用truncate time函数将datetime转换为日期。

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());
实体功能