mezoid
mezoid

Reputation: 28730

How do I limit the result set being pulled back by NHibernate's GetByCriteria?

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?

Upvotes: 3

Views: 3106

Answers (3)

CrazyCoderz
CrazyCoderz

Reputation: 1341

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();

    }

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039220

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

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502446

Use ICriteria.SetMaxResults()

Upvotes: 12

Related Questions