当我在where子句中对select中的同一表进行引用时,linq to Nhibernate会生成两个联接,一个用于select,一个用于where.即
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 };生成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:
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?
推荐答案您是否尝试过比较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.
更多推荐
Linq to NHibernate生成到同一表的多个联接
发布评论