Nhibernate 2.0 Efficient DataList Control and ObjectDataSource

How do I do what Scott did in one call using nHibernate 2 ObjectDataSource

http://weblogs.asp.net/scottgu/archive/2006/01/07/434787.aspx

below is my data access method

public IList GetListOfUser(int rows, int pageIndex) { IList userList = null; using (ITransaction tx = _session.BeginTransaction()) { try { userList = _session.CreateQuery("Select u from User u where u.DateSubmitted is not null") .SetFirstResult(rows * (pageIndex - 1) + 1) .SetMaxResults(rows) .List(); tx.Commit(); } catch (NHibernate.HibernateException ex) { tx.Rollback(); AppUtil.LogHelper.WriteLog(LogLevel.ERROR, ex.ToString()); throw; } } return userList; } 
+3
source share
2 answers

In fact, you can get the results page and the total number of records in one server call using this helper method if you use ICriteria queries:

  protected IList<T> GetByCriteria( ICriteria criteria, int pageIndex, int pageSize, out long totalCount) { ICriteria recordsCriteria = CriteriaTransformer.Clone(criteria); // Paging. recordsCriteria.SetFirstResult(pageIndex * pageSize); recordsCriteria.SetMaxResults(pageSize); // Count criteria. ICriteria countCriteria = CriteriaTransformer.TransformToRowCount(criteria); // Perform multi criteria to get both results and count in one trip to the database. IMultiCriteria multiCriteria = Session.CreateMultiCriteria(); multiCriteria.Add(recordsCriteria); multiCriteria.Add(countCriteria); IList multiResult = multiCriteria.List(); IList untypedRecords = multiResult[0] as IList; IList<T> records = new List<T>(); if (untypedRecords != null) { foreach (T obj in untypedRecords) { records.Add(obj); } } else { records = new List<T>(); } totalCount = Convert.ToInt64(((IList)multiResult[1])[0]); return records; } 

It clones your original criteria twice: one criterion that returns records for the page and one criterion for the total number of records. It also uses IMultiCriteria to make both database calls in one reverse direction.

+3
source

He has a job, but two challenges

I added itemCount to the link:

 itemCount = (Int64)_session.CreateQuery("select count(UserId) from User") .UniqueResult(); 
0
source

All Articles