2011-11-16 31 views
15

我有这样的LINQ to NHibernate的重复加入

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); 

的查询where子句必须取后因this bug。 问题是,拨号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提供程序中的错误吗?也许有一个解决方法?

回答

4

Added issue狡猾提到的,这是一个已知问题的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); 
1

不能完全确定,但删除您那里查询,而是使用上的

线联接东西在communityId x.CommunityMember.Community加盟澳等于x.communityMember.Community.Id(我的语法是comp0letely出,但可以在NHibernate的JIRA

1

为您服务作为提示)