【问题标题】:LINQ to Nhibernate duplicates joinsLINQ to Nhibernate 重复连接
【发布时间】:2014-01-15 01:11:30
【问题描述】:

我有这样的查询

var orderedQueryable = this.participationRequests
           .Fetch(x => x.CommunityEvent)
           .Fetch(x => x.CommunityMember)
                .ThenFetch(x => x.User)
           .Where(x => x.CommunityMember.Community.Id == communityId)
           .OrderBy(x => x.CreateDate);

由于this bug,where 子句需要在 fetch 之后。 问题是 thouse Fetch 调用会发出额外的连接。在 SQL 中查询如下所示:

select *
from   ParticipationRequests participat0_
       left outer join CommunityEvents communitye1_
         on participat0_.CommunityEventId = communitye1_.Id
       left outer join CommunityMembers communitym2_
         on participat0_.CommunityMemberId = communitym2_.Id
       left outer join Users user3_
         on communitym2_.UserId = user3_.Id
       inner join CommunityMembers communitym4_
         on participat0_.CommunityMemberId = communitym4_.Id
       inner join CommunityMembers communitym5_
         on participat0_.CommunityMemberId = communitym5_.Id
       inner join Communities community6_
         on communitym5_.CommunityId = community6_.Id
where  community6_.Id = 2002 /* @p0 */
order  by participat0_.CreateDate asc

它执行内连接以在CommunityId 上设置条件,并执行左外连接以进行获取。

我找到了similar question,但我的查询有不同的执行计划,有和没有额外的连接。

这是 LINQ 提供程序中的错误吗?也许有解决方法?

【问题讨论】:

    标签: c# .net linq nhibernate linq-to-nhibernate


    【解决方案1】:

    Added issue 在 nhibernate jira 上

    【讨论】:

      【解决方案2】:

      不完全确定,但删除您的 where 查询,而是在

      行中使用 join 的东西

      在 communityId 上的 x.CommunityMember.Community 中加入 o 等于 x.communityMember.Community.Id(我的语法完全没有,但可以作为提示)

      【讨论】:

        【解决方案3】:

        正如 Sly 提到的,这是 Linq to NHibernate 的一个已知问题。

        我能够通过使用 HQL 而不是 Linq 来解决这个问题。您的结果将类似于:

        CommunityEvent ce = null;
        CommunityMember cm = null;
        var queryable = this.participationRequests
            .JoinAlias(x => x.CommunityEvent, () => ce)
            .JoinAlias(x => x.CommunityMember, () => cm)
            .Where(() => cm.Community.Id == communityId)
            .OrderBy(x => x.CreationDate);
        

        【讨论】:

          猜你喜欢
          • 2014-04-08
          • 1970-01-01
          • 2011-03-19
          • 1970-01-01
          • 2010-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多