Linq to NHibernate生成到同一表的多个联接 [英] Linq to NHibernate generating multiple joins to the same table

查看:261
本文介绍了Linq to NHibernate生成到同一表的多个联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在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生成到同一表的多个联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆