DbArithmeticExpression参数必须具有数字通用types
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(); 这个Linqexpression式抛出这个exception:
 DbArithmeticExpression arguments must have a numeric common type. 
请帮忙!
 entity framework中不支持使用DateTime算术。 你必须使用DbFunctions *。 所以,对于你的陈述的第一部分,像这样的: 
 var sleeps = context.Sleeps(o => DbFunctions.DiffHours(o.ClientDateTimeStamp, clientDateTime) < 24); 
 请注意, DiffHours方法接受Nullable<DateTime> 。 
  entity framework版本6之前的EntityFunctions 。 
 我知道这是一个古老的问题,但在你的具体情况下,而不是像DBFunctions所build议的那样使用DBFunctions,难道你不能把操作从Lambda中移出来吗? 
 毕竟clientDateTime和time24是固定值,所以他们的差异不需要在每次迭代中重新计算。 
喜欢:
 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(); 
如果您试图将存储的date时间与固定时间戳相比较,并与其他date时间进行比较,则通常可以使用此重构。