[英]NHibernate HQL's Equivalent to T-SQL's TOP Keyword
What is NHibernate HQL's Equivalent to T-SQL's TOP Keyword? 什么是NHibernate HQL相当于T-SQL的TOP关键字?
Also what is the non-HQL way for saying give me the first 15 of a class? 还有什么是非HQL的说法,给我一个班级的前15个?
It's actually pretty easy in HQL: 在HQL中实际上非常简单:
var top15 = session.CreateQuery("from SomeEntity")
.SetFirstResult(0)
.SetMaxResults(15)
.List<SomeEntity>();
Don't know how to do this using the criteria API though. 但是,不知道如何使用条件API执行此操作。
Criteria API Method: 标准API方法:
ICriteria criteria = DaoSession.CreateCriteria(typeof(T));
criteria.SetFirstResult(StartIndex);
criteria.SetMaxResults(MaximumObjects);
return criteria.List<T>();
From NHibernate 3.2 you could use SKIP n / TAKE n
in hql at the end of the query. 从NHibernate 3.2开始,你可以在查询结束时在hql中使用
SKIP n / TAKE n
。 It could be very helpful in subqueries where you can not use SetMaxResults
. 在无法使用
SetMaxResults
子查询中,它可能非常有用。
For example: 例如:
select l, (select u from User u where u.Location = l order by u.Date asc take 1)
from Location l
For completeness, here is how to do it with the QueryOver
API introduced in NHibernate 3.0: 为了完整
QueryOver
,这里是如何使用NHibernate 3.0中引入的QueryOver
API:
var top15 = session.QueryOver<SomeEntity>().Take(15).List();
Throw in a .Skip(someInt)
if you need to define a start index, eg for paging. 如果需要定义起始索引(例如,用于分页
.Skip(someInt)
则抛出.Skip(someInt)
。
mookid8000 is giving false information. mookid8000提供虚假信息。
there is no way of setting SQL TOP N with HQL :( 没有办法用HQL设置SQL TOP N :(
it always downloads all of the table to .NET and the takes the TOP, wich is just plain stupid! 它总是将所有表格下载到.NET并且采用TOP,这简直是愚蠢的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.