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:
{
IList result = null;
string query_string = “from my_class”;
IMultiQuery query = session.CreateMultiQuery()
.Add(session.CreateQuery(query_string)
.SetFirstResult((page – 1) * pageSize)
.SetMaxResults(pageSize))
.Add(“select count(*) ” + query_string);
IList list = query.List();
result = (IList)list[0];
count = (long)((IList)list[1])[0];
return result;
}
LINQ-to-NHibernate:
{
var q = from t in session.Linq
select t;
return q
.Skip((page – 1) * pageSize)
.Take(pageSize)
.ToList
}
Castle ActiveRecord:
{
Order[] orders;
if (string.IsNullOrEmpty(sortExpression))
orders = new Order[0];
else
{
orders = new Order[1];
const string DESC = ” DESC”;
if (sortExpression.EndsWith(DESC))
orders[0] = Order.Desc(sortExpression.Replace(DESC, string.Empty));
else
orders[0] = Order.Asc(sortExpression);
}
return SlicedFindAll(startRowIndex, maximumRows, orders);
}
public static int TotalCount()
{
return Count();
}