How to limit the result set returned by NHibernate GetByCriteria?

I have an NHibernate Dao..lets called MyClassDao for a better name.

I am writing the following code.

MyClassDao myDao = new MyClassDao();

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

I use this in Unit Test to return values ​​from a database. However, to run a test that is too long, in my opinion, takes more than 30 seconds ... so what I would like to do is limit the result set pulled backward ... say, to 5 values.

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

set rowcount 5
select * from whatever_table
set rowcount 0

Is there a way ... without using NHibernate's query language to limit the size of the result set?

+3
source share
3

ICriteria.SetMaxResults()

+12

SetMaxResults IQuery ( HQL) ICriteria ( API ).

+2

I have something like this in my repository to help with unrelated 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();

    }
0
source

All Articles