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()方法

就可以了。。。。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM