2011-11-16 83 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

爲您服務作爲提示)