Linq to sql中使用DateDiff()

 Linq to sql中使用DateDiff()

计算时间差的方法

第一种办法:

from p in PurchaseLists where EntityFunctions.DiffDays(p.CreateTime,DateTime.Now) >=(p.DayLen/2) select p

报错:System.NotSupportedException: LINQ to Entities 不识别方法“System.Nullable`1[System.Int32] DiffDays(System.Nullable`1[System.DateTime], System.Nullable`1[System.DateTime])”,因此该方法无法转换为存储表达式。

第二种方法:

PurchaseLists.Where(t=>(DateTime.Now-t.CreateTime).Days>=p.DayLen/2)

报错 DbArithmeticExpression参数必须具有数值通用类型

 

这时候,就是因为你使用的是EF6框架。上面两种方法都不行。

将EntityFunctions.DiffDays()替换为System.Data.Entity.DbFunctions.DiffDays()方法

就可以了。。。。

 

posted @ 2018-12-26 09:26  Melody.Gkx  阅读(1928)  评论(0编辑  收藏  举报