Linq to NHibernate生成到同一表的多个联接

编程入门 行业动态 更新时间:2024-10-27 12:36:21
本文介绍了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生成到同一表的多个联接

本文发布于:2023-05-26 16:20:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   Linq   NHibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!