简体   繁体   中英

using nhibernate, how would i construct a query that wants the most recent 50 rows

i have a table called orders and i have a column called Last Update (and an order object with a LastUpdate property). I want to construct a query using nhibernate to get the last 50 rows so i don't go to the database and get everything and then have to filter results in my application.

is this possible in nhibernate. I am trying to use the LINQ api

如果您使用的是Criteria,则使用SetMaxResults(50)并对日期时间进行降序排序。

Here's the LINQ version of this query.

var orders = session.Query<Order>()
    .OrderByDescending(x => x.LastUpdate)
    .Take(50);

Here's the screen shot of the code sample...

代码示例

Here's the screen shot from NHibernate Profiler...

NHibernate探查器示例

你可以使用SetMaxResults(50) ,虽然取决于你想要的50行(最新?第一?最后?),你可能也需要做一个SortBy表达式。

var orders = session.Query<Linq>()
    .OrderByDescending(x => x.LastUpdate)
    .Take(50);

In general case suggesing LastUdate can be nullable using Linq2SQL you may write extension method to your IQueriable:

public static partial class FooTable
{
    public static IQueryable<FooTable> LastUpdated(this IQueryable<FooTable> queryable, int count)
    {
        return queryable.Where(x => (x.LastUdate != null))
            .OrderByDescending(x => x.LastUdate)
            .Take(count);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM