我有这样的查询
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子句需要在获取之后。
问题是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提供程序中的错误吗?也许有一种解决方法?
答案 0 :(得分:4)
答案 1 :(得分:1)
不完全确定,但删除你的where查询,而是使用
行上的连接在communityId上的x.CommunityMember.Community中加入o等于x.communityMember.Community.Id(我的语法完全没出来,但可以作为提示提供给你)
答案 2 :(得分:1)
正如Sly所说,这是Linq对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);