Paging methods

ASP.NET GridView and other similar controls that support paging needs special methods that return only the requested slice of data and  the count of total data. As I’m a lover of NHibernate/Castle ActiveRecord I have gathered paging techniques in NHibernate HQL, LINQ-to-NHibernate and Castle ActiveRecord:

NHIbernate HQL:

public IList GetData(int page, int pageSize, ref long count)
    IList result = null;
    string query_string = “from my_class”;
    IMultiQuery query = session.CreateMultiQuery()
        .SetFirstResult((page – 1) * pageSize)
        .Add(“select count(*) ” + query_string);
    IList list = query.List();
    result = (IList)list[0];
    count = (long)((IList)list[1])[0];
    return result;


internal List GetData(int page, int pageSize)

    var q = from t in session.Linq()
            select t;

    return q
        .Skip((page – 1) * pageSize)

Castle ActiveRecord:

public static Company[] FindAll(int maximumRows, int startRowIndex, string sortExpression)
    Order[] orders;

    if (string.IsNullOrEmpty(sortExpression))
        orders = new Order[0];
        orders = new Order[1];
        const string DESC = ” DESC”;
        if (sortExpression.EndsWith(DESC))
            orders[0] = Order.Desc(sortExpression.Replace(DESC, string.Empty));
            orders[0] = Order.Asc(sortExpression);

    return SlicedFindAll(startRowIndex, maximumRows, orders);

public static int TotalCount()
    return Count();

For more info refer to this link, link, link and this link.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *