简体   繁体   English

使用nhibernate,我将如何构造一个想要最近50行的查询

[英]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). 我有一个名为orders的表,我有一个名为Last Update的列(以及一个具有LastUpdate属性的订单对象)。 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. 我想使用nhibernate构造一个查询来获取最后50行,所以我不去数据库并获取所有内容,然后必须在我的应用程序中过滤结果。

is this possible in nhibernate. 这是否可能在nhibernate。 I am trying to use the LINQ api 我正在尝试使用LINQ api

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

Here's the LINQ version of this query. 这是此查询的LINQ版本。

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 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: 一般情况下,使用Linq2SQL建议LastUdate可以为空,您可以将扩展方法写入您的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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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