【问题标题】:Can't enumerate LinQ results with left join无法使用左连接枚举 LinQ 结果
【发布时间】:2010-05-20 02:32:59
【问题描述】:
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.IEnumerable1[SCEFramework.Models.fileStorage] DefaultIfEmpty[fileStorage](System.Collections.Generic.IEnumerable1[SCEFramework.Models.fileStorage])' 方法,而这种方法不能 翻译成商店表达式

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

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

【问题讨论】:

    标签: linq linq-to-entities


    【解决方案1】:

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

    from fileItem in objFile.DefaultIfEmpty()
    

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

    尝试删除DefaultIfEmpty 调用,看看会发生什么。

    【讨论】:

    • 非常感谢您的解决方案。我删除了 objFile.DefaultIfEmpty() 然后效果很好。
    • 如果回答了您的问题,请将此回复标记为答案。 :)
    • 我会投票给你这个乔恩,因为它是正确的答案:-)
    猜你喜欢
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    相关资源
    最近更新 更多