我已經創建了下面的函數來獲取日期差:LINQ的靜態方法錯誤
public static double MonthsDifference(DateTime dtStart, DateTime dtNow)
{
DateTime start = dtStart;
DateTime end = dtNow;
int compMonth = (end.Month + end.Year * 12) - (start.Month + start.Year * 12);
double daysInEndMonth = (end - end.AddMonths(1)).Days;
double months = compMonth + (start.Day - end.Day)/daysInEndMonth;
return months;
}
我使用它在我的LINQ查詢
var query = from c in context.AccountOwners.Where(MonthsDifference(p.Account.StateChangeDate,DateTime.Now) < 4)
select c;
return query.Count();
,但它給錯誤:
LINQ實體無法識別方法'Double MonthsDifference(System.DateTime,System.DateTime)'方法,並且此方法無法轉換爲商店表達式。
請建議的解決方案
首先。你可以在linq中創建和使用函數來調用實體。然而,從我的研究結果來看,只有在您嘗試使用相同類的方法中聲明該方法時纔有效。第二個是 – Michael 2014-05-24 01:39:34
。使用日期/時間函數的內置實體函數。 – Michael 2014-05-24 01:39:57