1. 程式人生 > >Linq to sql中使用DateDiff()

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

就可以了。。。。