【发布时间】:2013-02-22 09:29:29
【问题描述】:
我在当前项目中刚开始使用 NHibernate(使用 SQLite),我主要使用 Query<>,因为我熟悉在 Linq 中编写 db 查询。
当我遇到一些更复杂的查询时,我对QueryOver<> 进行了一些研究,并认为它应该优于Query<>,因为“QueryOver 语法是 NH 特定的”。此外,似乎没有什么是Query<> 可以做到的,而QueryOver<> 却无法做到。
所以我开始相应地替换 Query<> 的所有用法。不久之后,我遇到了第一个“问题”,使用Query<> 似乎更方便。
示例(从表BillingDataEntity 中的列CustomNumber 中选择最大值):
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 版本更易于阅读。我的查询完全错误,或者换句话说:有更好的方法吗?
我查看了生成的 SQL 输出:
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 进一步转换为实际数据库查询的“内部”(方法相关)查询吗?
【问题讨论】:
标签: c# nhibernate