我有一个设备型号:
public class DeviceModel
{
public DateTime Added { get;set; }
}
我希望能按添加日期
(仅限日期,而不是日期和时间)对设备数量进行分组选择。但我的当前实现无效,因为linq无法将DateTime.Date
翻译成sql:
var result = (
from device in DevicesRepository.GetAll()
group device by new { Date = device.Added.Date } into g
select new
{
Date = g.Key.Date,
Count = g.Count()
}
).OrderBy(nda => nda.Date);
如何更改此查询以使其正常工作?
using System.Data.Entity;
中的DbFunctions.TruncateTime()
。 - Christian Gollhardt