Grunf
Grunf

Reputation: 472

query from nhibernate Criteria api to linq

I want to translate following query from nhibernate criteria query api to linq.

 var userquery = session.CreateCriteria(typeof(User))
          .SetFirstResult(pageIndex * pageSize)
          .SetMaxResults(pageSize);

 var totalcountQuery = CriteriaTransformer.Clone(userquery)
           .SetProjection(Projections.RowCountInt64());

Thanks

Update

IEnumerable<User> dbUsers = userquery.Future<User>();
IFutureValue<long> count = totalcountQuery.FutureValue<long>();

Upvotes: 0

Views: 778

Answers (1)

Martin Ernst
Martin Ernst

Reputation: 5679

A direct(ish) translation would be:

var userQuery = session.Query<User>().Skip(pageIndex * pageSize).Take(pageSize);

var totalCount = userQuery.LongCount();

However I'm not sure why you'd want to do the count after the Skip & Take, I would think something like:

var totalCount = session.Query<User>().LongCount(); 

would be closer to what you want

See http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Executing-future-queries-with-NHibernate-Linq.aspx

For futures on Linq, you could do:

var users = userQuery.ToFuture();    
var totalCount = userQuery.LongCount(); // users will be a future, count won't be but if it's only 2 queries then this will execute them both

Upvotes: 1

Related Questions