LINQ例外:方法 ... 沒有支援的轉譯為 SQL (使用自定Function篩選或排序)

 DC是LINQ TO SQL的資料來源, DC.BOOK是其中的TABLE, 若以下程式碼執行後

01 //DC = DataContext (LINQ TO SQL)
02
03 IEnumerable<BOOK> books
04                 = from b in DC.BOOK
05                   where CheckCount(b)
06                   orderby (int)GetPrize(b) descending
07                   select b;
08
09 private static bool CheckCount(BOOK b)
10 {
11     //code....
12     return true;
13 }

 

會出現例外狀況: 方法 'Boolean CheckCount(BOOK)' 沒有支援的轉譯為 SQL。

此例外會使where無法進入自定函式去執行篩選 (orderby 的 GetPrize也會發生同樣情形)

原因可能在於DC.BOOK並非IEnumerable<>型態,故沒有實作Where的Function功能

 解決方式:from b in DC.BOOK 改成 from b in DC.BOOKS.AsEnumerable<BOOK>()

將資料來源轉換成IEnumerable<>型態即可。

Add Comment DotBlogs Tags: ADO LINQ 閱讀數 : 88 訂閱
 

回覆

目前沒有回應.

標題*
姓名*
電子郵件 (never displayed)
 
個人網頁
回覆*

登入後使用進階評論
Please add 2 and 1 and type the answer here: