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);
由于 这个 bug,where 子句需要在 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
上添加条件,并执行左外连接以执行获取。
我发现类似的问题,但是我的查询有不同的执行计划,有或没有额外的连接。
这是 LINQ 提供程序中的错误吗?也许有解决方法?
I have a query like this
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);
The where clause needs to be after fetch due to this bug.
The problem is that thouse Fetch
calls issue additional joins. In SQL query looks like the following:
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
It does inner join to put a condition on CommunityId
and does left outer join to do fetching.
I've found similar question, but my query has different execution plan with and without additional joins.
Is it a bug in LINQ provider? Maybe there is a workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 nhibernate jira 上添加了问题
Added issue on nhibernate jira
不完全确定,但删除您的 where 查询,而是使用 join o in x.CommunityMember.Community 行上的 join 内容
on communityId equals x.communityMember.Community.Id (我的语法已完全退出,但可能会为您提供提示)
Not exactly sure but remove your where query and instead use a join something on the lines of
join o in x.CommunityMember.Community on communityId equals x.communityMember.Community.Id (my syntax is comp0letely out, but may serve you as a hint)
正如 Sly 提到的,这是 Linq to NHibernate 的一个已知问题。
我可以通过使用 HQL 而不是 Linq 来解决这个问题。你的结果会是这样的:
As Sly mentioned, this is a known issue with Linq to NHibernate.
I was able to work around this by using HQL instead of Linq. Your result would be something like: