DbArithmeticEx pression参数必须有一​​个数字常见类型 [英] DbArithmeticExpression arguments must have a numeric common type

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

问题描述

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的前pression引发此异常:

This Linq expression throws this exception:

DbArithmeticExpression arguments must have a numeric common type.

请帮助!

推荐答案

算术的DateTime 是不是在实体框架的支持。你必须使用 DbFunctions *。因此,对于您语句的第一部分,是这样的:

Arithmetic with DateTime is not supported in Entity Framework. 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 方法接受可空&LT; D​​ateTime的&GT;

<子> * <一href="http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions.aspx"><$c$c>EntityFunctions之前的实体框架版本6。

这篇关于DbArithmeticEx pression参数必须有一​​个数字常见类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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