无法使用左连接枚举LinQ结果

时间:2010-05-20 02:32:59

标签: linq linq-to-entities

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 to Entities无法识别   方法   '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;    
        }

请建议我任何解决方案。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

我认为这个问题是以下'from'子句:

from fileItem in objFile.DefaultIfEmpty()

请注意,您的LINQ查询只能在执行结果集合时执行。因此,当你认为你的异常在foreach中时,它实际上在你的表达中。这会中断,因为objFile.DefaultIfEmpty()的可能空值无法转换为IEnumerable

尝试删除DefaultIfEmpty电话,看看会发生什么。

相关问题