Question

I have an NHibernate Dao..lets call it MyClassDao for want of a better name.

I am writing the following code.

MyClassDao myDao = new MyClassDao();

var values = myDao.GetByCriteria(Restrictions.Eq("Status", someStatusValue));

I am using this in a Unit Test to pull back values from the database. However, it is taking over 30 seconds to run the test which is too long in my opinion...so what I'd like to do is limit the result set being pulled back...say to about 5 values.

in sql I would do something like the following to to achieve something like this

set rowcount 5
select * from whatever_table
set rowcount 0

Is there a way...without using the NHibernate Query language to restrict the size of a result set?

Was it helpful?

Solution

Use ICriteria.SetMaxResults()

OTHER TIPS

You can use the SetMaxResults method on IQuery (if you use HQL) or ICriteria (if you use criteria API).

I have something like this in my repository to help with the unbound result sets

public IQueryable<T> SelectWithLimit<T>(int maxResults) where T : class
    {
        ICriteria criteria = SessionWrapper.Session.CreateCriteria(typeof (T)).SetMaxResults(maxResults);
        return criteria.List<T>().AsQueryable();

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top