TimeSpan time24 = new TimeSpan(24, 0, 0);
TimeSpan time18 = new TimeSpan(18, 0, 0);
// first get today's sleeping hours
List<Model.Sleep> sleeps = context.Sleeps.Where(
o => (clientDateTime - o.ClientDateTimeStamp < time24) &&
o.ClientDateTimeStamp.TimeOfDay > time18 &&
clientDateTime.TimeOfDay < time18 &&
o.UserID == userid).ToList();
这个 Linq 表达式抛出这个异常:
DbArithmeticExpression arguments must have a numeric common type.
请帮忙!
用DateTime
算术Entity Framework 6 及更早版本不支持。你必须使用 DbFunctions *。所以,对于你陈述的第一部分,是这样的:
var sleeps = context.Sleeps(o =>
DbFunctions.DiffHours(o.ClientDateTimeStamp, clientDateTime) < 24);
请注意 DiffHours
方法接受 Nullable<DateTime>
.
Entity Framwork 核心(与 Sql Server 一起使用时,可能还有其他数据库提供程序)支持 DateTime AddXxx
功能(如 AddHours
)。它们被翻译成 DATEADD
在 SQL 中。
<子>* EntityFunctions
在 Entity Framework 版本 6 之前。
我是一名优秀的程序员,十分优秀!