从 nhibernate Criteria API 到 linq 的查询

本文关键字:linq 查询 API nhibernate Criteria | 更新日期: 2023-09-27 18:35:16

我想将以下查询从 nhibernate criteria query api 转换为 linq。

 var userquery = session.CreateCriteria(typeof(User))
          .SetFirstResult(pageIndex * pageSize)
          .SetMaxResults(pageSize);
 var totalcountQuery = CriteriaTransformer.Clone(userquery)
           .SetProjection(Projections.RowCountInt64());

谢谢

更新

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

从 nhibernate Criteria API 到 linq 的查询

直接(ish)翻译是:

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

但是,我不确定为什么要在Skip & Take之后进行计数,我会这样想:

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

会更接近你想要的

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

对于 Linq 上的期货,您可以执行以下操作:

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