我有一個問題需要花費數週才能解決,而我一直無法解決。Linq to Entities不能識別方法System.DateTime ..並且無法將其轉換爲商店表達式
我有一個類,我有兩種方法。以下是應該從數據庫中獲取最新日期。這個日期代表客戶做了以「東西」最新的支付:
public DateTime getLatestPaymentDate(int? idCustomer)
{
DateTime lastDate;
lastDate = (from fp in ge.Payments
from cst in ge.Customers
from brs in ge.Records.AsEnumerable()
where (cst.idCustomer == brs.idCustomer && brs.idHardBox == fp.idHardbox
&& cst.idCustomer == idCustomer)
select fp.datePayment).AsEnumerable().Max();
return lastDate;
}//getLatestPaymentDate
在這裏,我有另一種方法,這是應該叫上一個完成LINQ查詢,並把它傳遞到Crystal報告:
//Linq query to retrieve all those customers'data who have not paid their safebox(es) annuity in the last year.
public List<ReportObject> GetPendingPayers()
{
List<ReportObject> defaulterCustomers;
defaulterCustomers = (from c in ge.Customer
from br in ge.Records
from p in ge.Payments
where (c.idCustomer == br.idCustomer
&& br.idHardBox == p.idHardBox)
select new ReportObject
{
CustomerId = c.idCustomer,
CustomerName = c.nameCustomer,
HardBoxDateRecord = br.idHardRecord,
PaymentDate = getLatestPaymentDate(c.idCustomer),
}).Distinct().ToList();
}//GetPendingPayers
沒有編譯錯誤,在這裏拋出,但是當我運行應用程序,第二種方法試圖調用該領域的第一個PaymentDate
在標題中提到的錯誤發生:
Linq to Entities does not recognize the method System.DateTime.. and cannot translate this into a store expression
請有人提供一個有用的輸入,使我從這個凌亂的錯誤?任何幫助將不勝感激 !
非常感謝!
嗨,感謝您的輸入,但我真的無法完成這項工作。你說你提供的第二個問題是最簡單的,但是我不明白它是如何解決我的問題的,因爲我已經聲明瞭一個DateTime類型的變量並將其解析爲假設的日期,但仍然不起作用。你能再解釋一下嗎?我將非常感激! – 2013-03-14 22:26:58
請檢查我上面的評論。非常感謝! – 2013-03-15 13:47:59
當然!簡單地說:'getLatestPaymentDate'沒有SQL等價物。我會編輯我的答案,以便更明確地說明你可以做什麼。 – SolarBear 2013-03-15 14:38:25