2011-11-16 3 views
15

私はこのようなクエリを持っています重複する結合をNhibernateするLINQ

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問題は、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プロバイダのバグですか?おそらく回避策がありますか?

答えて

1

正確にわからないが、x.communityMember.Community.Idに等しいcommunityId上x.CommunityMember.Community中のO参加

の行に何かを結合を使用代わりにあなたの場所のクエリを削除し、(私の構文はcomp0letely出ていますしかし、ヒントとしてお役に立てること)

1

スライが述べたように、これは、NHibernateはLINQのへの既知の問題です。

これを回避するには、Linqの代わりにHQLを使用しました。結果は次のようになります。

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); 
関連する問題