简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM