2013-03-08 29 views
3

我有一個問題需要花費數週才能解決,而我一直無法解決。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

請有人提供一個有用的輸入,使我從這個凌亂的錯誤?任何幫助將不勝感激 !

非常感謝!

回答

1

看一看這些問題:

LINQ to Entities does not recognize the method

LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method

基本上,你不能在C#端使用一個值,並將其轉換爲SQL。第一個問題提供了一個更徹底的解釋;第二個爲您的問題提供了一個簡單的解決方案

編輯:

簡而言之:在EF被要求SQL服務器來執行getLatestPaymentDate方法,它沒有關於線索。您需要在程序端執行它。

只需先進行查詢,結果放入一個列表,然後在內存中的列表上做你的Select

List<ReportObject> defaulterCustomers; 

    var queryResult = (from c in ge.Customer 
         from br in ge.Records 
         from p in ge.Payments 

         where (c.idCustomer == br.idCustomer 
           && br.idHardBox == p.idHardBox)).Distinct().ToList(); 

defaulterCustomers = from r in queryResult 
        select new ReportObject 
         { 
          CustomerId = r.idCustomer, 
          CustomerName = r.nameCustomer, 
          HardBoxDateRecord = r.idHardRecord, 
          PaymentDate = getLatestPaymentDate(r.idCustomer), 
         }).Distinct().ToList(); 

我沒有訪問到你的代碼,很明顯,所以試試吧出來告訴我它是否適合你! 你最終會得到一個內存列表

+0

嗨,感謝您的輸入,但我真的無法完成這項工作。你說你提供的第二個問題是最簡單的,但是我不明白它是如何解決我的問題的,因爲我已經聲明瞭一個DateTime類型的變量並將其解析爲假設的日期,但仍然不起作用。你能再解釋一下嗎?我將非常感激! – 2013-03-14 22:26:58

+0

請檢查我上面的評論。非常感謝! – 2013-03-15 13:47:59

+0

當然!簡單地說:'getLatestPaymentDate'沒有SQL等價物。我會編輯我的答案,以便更明確地說明你可以做什麼。 – SolarBear 2013-03-15 14:38:25

相關問題