【问题标题】:Linq Query with Join not working on .Net 3.5 but works on .Net 4+Linq Query with Join 不适用于 .Net 3.5 但适用于 .Net 4+
【发布时间】:2013-05-24 13:44:36
【问题描述】:

在我添加连接之前,我的查询一直在 .Net 3.5 SP1 中运行。即使没有关联的用户实体,连接也允许从评论实体中进行选择。我收到以下错误:

LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable`1[XXX.YYY.BusinessLayer.User] DefaultIfEmpty[User](System.Collections.Generic.IEnumerable`1[XXX.YYY.BusinessLayer.User])' method, and this method cannot be translated into a store expression.

我使用 LinqPad 验证了查询在 .Net 4+(我认为!)中有效。

我在解决方案中的查询如下:

var comments = (from dic in _context.Comments
                        join u in _context.Users on dic.CommentUserId equals u.UserId into j1
                        from j2 in j1.DefaultIfEmpty()
                        where dic.Parent.Id == 406
                        select new { dic.CommentId, dic.CommentDate, dic.CommentText, j2.AccountName }).AsEnumerable()
                       .Select(x => new CommentInfo
                       {
                           CommentId = x.CommentId,
                           CommentDate = x.CommentDate,
                           CommentText = x.CommentText,
                           UserName = ActiveDirectoryUtil.GetUserDisplayName(x.AccountName)
                       });

我对 Linq 和 Lambda 还很陌生,所以还在寻找解决方法。 ActiveDirectoryUtil.GetUserDisplayName 是一个静态方法调用,用于查找以获取用户的显示名称。

添加以下行后问题出现了:

... into j1 
from j2 in j1.DefaultIfEmpty()

【问题讨论】:

    标签: linq join lambda


    【解决方案1】:

    .NET 3.5 Framework 中的实体框架不支持DefaultIfEmpty() 方法。

    【讨论】:

    • 嗨,Emre...我现在明白了。我不得不改变查询的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 2011-10-09
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多