Linq to NHibernate 生成到同一个表的多个联接
当我在 select 和 where 子句中引用同一个表时,linq to Nhibernate 会生成两个连接,一个用于 select,一个用于 where。 ie
from child in Session.Query<Child>()
where child.Parent.Name == "Bob"
select new Info
{
ParentAge = child.Parent.Age,
ChildName = child.Name
};
生成如下 SQL:
Select this_.Name,
parent1.Age
From Child this_
left join Parent parent1 on child.ParentId = parent1.Id,
Parent parent2
Where child.ParentId = parent2.Id and parent2.Name = 'Bob'
我本以为我应该得到更像这样的 SQL:
Select this_.Name,
parent1.Age
From Child this_
inner join Parent parent1 on child.ParentId = parent1.Id
Where parent1.Name = 'Bob'
有没有办法构建查询来得到这个? 有关系吗?
When I have a reference to the same table in my select as in my where clause, linq to Nhibernate generates two joins, one for the select and one for the where. i.e.
from child in Session.Query<Child>()
where child.Parent.Name == "Bob"
select new Info
{
ParentAge = child.Parent.Age,
ChildName = child.Name
};
Generates SQL like:
Select this_.Name,
parent1.Age
From Child this_
left join Parent parent1 on child.ParentId = parent1.Id,
Parent parent2
Where child.ParentId = parent2.Id and parent2.Name = 'Bob'
I would have thought I should get SQL more like:
Select this_.Name,
parent1.Age
From Child this_
inner join Parent parent1 on child.ParentId = parent1.Id
Where parent1.Name = 'Bob'
Is there a way to structure the query to get this?
Does it matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用透明标识符来阻止 NHibernate 执行此操作,以便您的查询如下所示:
You can prevent NHibernate from doing this by using a transparent identifier, so that your query looks like this:
您是否尝试过比较 SSMS 中每个查询的执行计划?如果在 SQL Server 中消除了重复的联接,那么就没有关系了。我发现在某些情况下,我认为生成的查询效率非常低,但经过优化后,它最终与查询完全相同,看起来要好得多。
Have you tried comparing the query execution plan for each in SSMS? If the duplicated join is eliminated in SQL Server, then it doesn't matter. I've found that to be the case in a few instances where I thought the generated query was going to be very inefficient, but after optimization it ends up exactly the same as a query that looks much better.