作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
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 之前。
关于c# - DbArithmeticExpression 参数必须具有数字通用类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9820401/
TimeSpan time24 = new TimeSpan(24, 0, 0); TimeSpan time18 = new TimeSpan(18, 0, 0); // first get
TimeSpan time24 = new TimeSpan(24, 0, 0); TimeSpan time18 = new TimeSpan(18, 0, 0); // first get
我试图删除表中所有超过 3 小时的项目,但出现以下错误... DbArithmeticExpression 参数必须具有数字通用类型。 // Method to clean items in bask
我是一名优秀的程序员,十分优秀!