C# DbArithmeticExpression 参数必须具有数字公共类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9820401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DbArithmeticExpression arguments must have a numeric common type
提问by Nawaz Dhandala
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();
This Linq expression throws this exception:
此 Linq 表达式引发此异常:
DbArithmeticExpression arguments must have a numeric common type.
Please Help!
请帮忙!
采纳答案by Gert Arnold
Arithmetic with DateTimeis not supported in Entity Framework 6 and earlier. You have to use DbFunctions*. So, for the first part of your statement, something like:
DateTimeEntity Framework 6 及更早版本不支持算术 with 。你必须使用DbFunctions*。因此,对于您声明的第一部分,例如:
var sleeps = context.Sleeps(o =>
DbFunctions.DiffHours(o.ClientDateTimeStamp, clientDateTime) < 24);
Note that the DiffHoursmethod accepts Nullable<DateTime>.
请注意,该DiffHours方法接受Nullable<DateTime>.
Entity Framwork core(when used with Sql Server, maybe other db providers) supports the DateTime AddXxxfunctions (like AddHours). They're translated into DATEADDin SQL.
实体框架核心(与 Sql Server 一起使用时,可能是其他数据库提供程序)支持 DateTimeAddXxx函数(如AddHours)。它们被翻译成DATEADDSQL。
*EntityFunctionsprior to Entity Framework version 6.
*EntityFunctions在实体框架版本 6 之前。
回答by SoonDead
I know that this is an old question but in your specific case instead of using DBFunctionsas suggested by @GertArnold , couldn't you just invert the operation move out the arithmetic in question from the Lambda?
我知道这是一个老问题,但在您的特定情况下,而不是DBFunctions按照@GertArnold 的建议使用,您不能将操作从 Lambda 中移出有问题的算术吗?
After all clientDateTimeand time24are fix values so their difference does not need to be recalculated in every iteration.
毕竟clientDateTime和time24是固定值,因此不需要在每次迭代中重新计算它们的差异。
Like:
喜欢:
TimeSpan time24 = new TimeSpan(24, 0, 0);
TimeSpan time18 = new TimeSpan(18, 0, 0);
var clientdtminus24 = clientDateTime - time24;
// first get today's sleeping hours
List<Model.Sleep> sleeps = context.Sleeps.Where(
o => (clientdtminus24 < o.ClientDateTimeStamp) &&
o.ClientDateTimeStamp.TimeOfDay > time18 &&
clientDateTime.TimeOfDay < time18 &&
o.UserID == userid).ToList();
This refactor is usually possible if you are trying to compare the stored datetime shifted by a fix timestamp with an other datetime.
如果您尝试将固定时间戳移动的存储日期时间与其他日期时间进行比较,则通常可以进行此重构。
回答by messed-up
The other way, if performance is not the true goal, you can try using AsEnumerable().
So, it would be like
另一方面,如果性能不是真正的目标,您可以尝试使用AsEnumerable(). 所以,它会像
List<Model.Sleep> sleeps = context.Sleeps.AsEnumerable().Where(....
Adding AsEnumerable() will convert the SQL query to entity and allows to run .Net functions on them. For more info, check here about AsEnumerable
添加 AsEnumerable() 会将 SQL 查询转换为实体并允许在其上运行 .Net 函数。有关更多信息,请在此处查看 AsEnumerable

