问题描述
在我当前的项目中,我刚开始使用NHibernate(使用SQLite),而我主要使用Query<>
,因为我很熟悉在Linq中编写数据库查询.
I just started with NHibernate (using SQLite) in my current project and I mostly used Query<>
, because I was familiar writing db queries in Linq.
当我遇到一些更复杂的查询时,我对QueryOver<>
进行了一些研究,发现它应该优于Query<>
,因为"QueryOver语法是特定于NH的" .此外,似乎没有什么Query<>
可以做到QueryOver<>
无法完成的事情.
When I was confronted with some more complex queries, I did some research on QueryOver<>
and figured that it should be favored over Query<>
because "QueryOver syntax is NH specific". Also, there seems to be nothing that Query<>
can do that QueryOver<>
can't accomplish.
因此,我开始相应地替换所有Query<>
用法.不久之后,我有了第一个问题",使用Query<>
似乎更方便.示例(从表BillingDataEntity
的列CustomNumber
中选择最大值):
So I began replacing all usages of Query<>
accordingly. It wasn't long before I had the first "issue" where using Query<>
seemed just more convenient.Example (select highest value from column CustomNumber
in table BillingDataEntity
):
int result = Session.Query<BillingDataEntity>().Select(x => x.CustomNumber).OrderByDescending(a => a).FirstOrDefault();
int result = Session.QueryOver<BillingDataEntity>().Select(x => x.CustomNumber).OrderBy(a => a.CustomNumber).Desc.Take(1).SingleOrDefault<int>();
我不喜欢将结果显式转换为int,而Query<>版本更易于阅读.我得到的查询完全错误吗,或者换句话说:有没有更好的方法呢?
What I dislike is the need to explicitly cast the result to int and that the the Query<> version is just easier to read. Am i getting the query totally wrong, or in other words: Is there a better way to do it?
我看了一下生成的SQL输出:
I took a look at the generated SQL output:
NHibernate: select billingdat0_.CustomNumber as col_0_0_ from "BillingDataEntity" billingdat0_ order by billingdat0_.CustomNumber desc limit 1
NHibernate: SELECT this_.CustomNumber as y0_ FROM "BillingDataEntity" this_ ORDER BY this_.CustomNumber desc limit @p0;@p0 = 1 [Type: Int32 (0)]
我到底在看什么?这是NHibernate进一步转换为实际数据库查询的内部"(方法相关)查询吗?
What exactly am i looking at? Is this the "internal" (method dependent) query that NHibernate further translates into the actual database query?
推荐答案
这里有很多关于QueryOver和Query的答案,简而言之:-
There are plenty of answers regarding QueryOver versus Query here on Stackoverflow but in a nutshell:-
LINQ(查询)是一种适用于IQueryable的标准查询方法, 不需要显式引用NHibernate,可以考虑 与ORM无关,因此遵循linq标准.当你 正确地指出,您不需要像现在一样强制转换为int 在结果中选择customNumber.
LINQ (Query) is a standard query method that works on IQueryable that doesn't need explicit references to NHibernate and can be considered more ORM agnostic and therefore follows the linq standard. As you rightly pointed out you do not need to cast to an int as you are selecting into the result the customNumber.
如果生成的SQL非常不同,那么您的简单示例会让我感到惊讶.
I would be very surprised for your simple example if the generated SQL was very different.
我是QueryOver
的忠实拥护者,但是随着Linq提供程序变得越来越成熟,然后我95%的查询都使用Query
,但是对于某些特定于Nhibernate的内容,我还是使用QueryOver
.无论哪种方式,我都建议使用性能分析工具来查看您可以使用的工具.
I was a big fan of QueryOver
but as the Linq provider is getting more mature then 95% of my queries I use Query
but for some Nhibernate specific stuff I resort back down to QueryOver
. Either way I recommend using a profiling tool to see what you can live with.
这篇关于NHibernate Query之间有什么区别? vs QueryOver<> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!