ROW_NUMBER() and nhibernate - finding an item's page

前端 未结 3 898
旧巷少年郎
旧巷少年郎 2021-01-01 04:54

given a query in the form of an ICriteria object, I would like to use NHibernate (by means of a projection?) to find an element\'s order, in a manner equivalent to using

3条回答
  •  醉酒成梦
    2021-01-01 05:53

    After trying to find an NHibernate based solution for this myself, I ultimately just added a column to the view I happened to be using:

    CREATE VIEW vw_paged AS
    SELECT ROW_NUMBER() OVER (ORDER BY Id) AS [Row], p.column1, p.column2
    FROM paged_table p
    

    This doesn't really help if you need complex sorting options, but it does work for simple cases.

    A Criteria query, of course, would look something like this:

        public static IList GetRange(string search, int rows)
        {
            var match = DbSession.Current.CreateCriteria()
                .Add(Restrictions.Like("Id", search + '%'))
                .AddOrder(Order.Asc("Id"))
                .SetMaxResults(1)
                .UniqueResult();
    
            if (match == null)
                return new List();
            if (rows == 1)
                return new List {match};
    
            return DbSession.Current.CreateCriteria()
                .Add(Restrictions.Like("Id", search + '%'))
                .Add(Restrictions.Ge("Row", match.Row))
                .AddOrder(Order.Asc("Id"))
                .SetMaxResults(rows)
                .List();
        }
    

提交回复
热议问题