2010-05-20 56 views
1
var itemSet = from item in da.GetList<Models.account>() 
          join file in objFileStorageList 
          on item.account_id equals file.parent_id into objFile 
          from fileItem in objFile.DefaultIfEmpty() 
          where item.company != null && item.company.company_id == 123 
          orderby item.updatedDate descending 
          select 
          new 
          { 
           Id = item.account_id, 
           RefNo = item.refNo, 
           StartDate = item.StartDate , 
           EndDate = item.EndDate , 
           Comment = item.comment, 
           FileStorageID = fileItem != null ? fileItem.fileStorage_id : -1, 
           Identification = fileItem != null ? fileItem.identifier : null, 
           fileName = fileItem != null ? fileItem.file_nm : null 
          }; 

當我嘗試枚舉來自上面的Linq查詢的收集結果時,它會引發錯誤消息。使用左連接無法枚舉LinQ結果

LINQ實體無法識別 方法 'System.Collections.Generic.IEnumerable 1[SCEFramework.Models.fileStorage] DefaultIfEmpty[fileStorage](System.Collections.Generic.IEnumerable 1 [SCEFramework.Models.fileStorage])' 方法,並且該方法不能 翻譯成商店表達

foreach (var item in itemSet) 
     { 
      string itemRef= item.RefNo;  
     } 

請給我建議任何解決方案。 在此先感謝。

回答

2

我覺得這個問題是「從」以下條款:

from fileItem in objFile.DefaultIfEmpty() 

請注意,您的LINQ查詢只能在執行結果集的時間來執行。所以,當你認爲你的異常是在foreach中時,它實際上就在你的表達中。這會中斷,因爲objFile.DefaultIfEmpty()的可能空值不能投射到IEnumerable

嘗試刪除DefaultIfEmpty調用,看看會發生什麼。

+0

非常感謝您的解決方案。我已經刪除objFile.DefaultIfEmpty()然後它很好。 – nvtthang 2010-05-20 03:21:44

+0

如果回答您的問題,請將此回覆標記爲答案。 :) – 2010-05-20 07:45:03

+0

我會爲你投這個喬恩,因爲這是正確的答案:-) – 2010-11-18 09:21:44