查询从nhibernate Criteria api到linq

时间:2012-05-25 12:39:30

标签: c# linq nhibernate nhibernate-criteria

我想将以下查询从nhibernate条件查询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>();

1 个答案:

答案 0 :(得分:1)

直接(ish)翻译将是:

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

var totalCount = userQuery.LongCount();

然而,我不确定为什么你想要在Skip&amp;之后做点算。拿,我会想像:

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