DarthVader
DarthVader

Reputation: 55112

MVC 3 with NHibernate Pagination

I have find some questions and answer to how to do pagination with NHibernate, I have a code as follows:

public virtual List<T> GetPageResults(int pageIndex, int pageSize)
        {
            using (var session = SessionFactory.OpenSession())
            {
                var returnVal =
                    session.CreateCriteria<T>().SetFirstResult(pageIndex).SetMaxResults(pageSize).Future
                        <T>().OrderByDescending(x => x.Id).ToList();

                return returnVal;
            }
        }

As you can see I m doing the pagination with OrderByDescending, I want to display the last item first. Is this a good way of doing this?

Also, is there any framework i can use that will provide me the pagination easily, I see lot of packages but most of them has close to no documentation except SO questions.

thanks.

Upvotes: 1

Views: 514

Answers (1)

Anton
Anton

Reputation: 1583

This query you can write with QueryOver:

session.QueryOver<T>()
  .OrderBy(t => t.Property).Desc
  .Skip(pageIndex).Take(pageSize)
  .List();

Upvotes: 1

Related Questions