DbArithmeticExpression 参数必须具有数字通用类型 [英] DbArithmeticExpression arguments must have a numeric common type

查看:23
本文介绍了DbArithmeticExpression 参数必须具有数字通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 表达式抛出这个异常:

This Linq expression throws this exception:

DbArithmeticExpression arguments must have a numeric common type.

请帮忙!

推荐答案

带有 DateTime 的算术在 Entity Framework 6 及更早版本中不受支持.您必须使用 DbFunctions*.因此,对于您声明的第一部分,例如:

Arithmetic with DateTime is not supported in Entity Framework 6 and earlier. You have to use DbFunctions*. So, for the first part of your statement, something like:

var sleeps = context.Sleeps(o =>
    DbFunctions.DiffHours(o.ClientDateTimeStamp, clientDateTime) < 24);

请注意 DiffHours 方法接受 Nullable.

Entity Framwork 核心(当与 Sql Server 一起使用时,也许其他数据库提供程序)支持 DateTime AddXxx 函数(如 AddHours).它们在 SQL 中被翻译成 DATEADD.

Entity Framwork core (when used with Sql Server, maybe other db providers) supports the DateTime AddXxx functions (like AddHours). They're translated into DATEADD in SQL.

*EntityFunctions.

这篇关于DbArithmeticExpression 参数必须具有数字通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆