2012-05-25 89 views
0

我想從nhibernate標準查詢api轉換到linq以下查詢。查詢從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

直接(ISH)的翻譯是:

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

var totalCount = userQuery.LongCount(); 

但是我不知道爲什麼你會想要做的計數後跳過&採取,我會想這樣的:

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 
+0

我需要實現自定義的成員提供,感謝儘管 – Grunf

+0

問題更新,你可以翻譯這個linq中增加了兩行,謝謝 – Grunf