你潛在的解決方案將在一個事務中進行處理,但將是兩個數據庫調用。如果您必須只有一個數據庫調用,則應該使用多點查詢/將來查詢作爲對等建議。有關未來語法的更多信息,請查看此帖:http://ayende.com/blog/3979/nhibernate-futures。
下面是實現您的方案的幾種方法...
QueryOver(2調用數據庫):
var query = session.QueryOver<Organism>();
var result = query
.Skip((Page - 1) * PageSize)
.Take(PageSize)
.List();
var rowcount = query.RowCount();
隨着樣本集100個生物,以及查詢生物體11-20 ,這裏被髮送到數據庫的兩個查詢:
SELECT TOP (@p0) Id0_0_, Title0_0_ FROM (SELECT this_.Id as Id0_0_, this_.Title as Title0_0_, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM Organism this_) as query WHERE query.__hibernate_sort_row > @p1 ORDER BY query.__hibernate_sort_row;@p0 = 10 [Type: Int32 (0)], @p1 = 10 [Type: Int32 (0)]
SELECT count(*) as y0_ FROM Organism this_
QueryOver(1級分貝的呼叫與Future):
var query = session.QueryOver<Organism>()
.Skip((Page - 1) * PageSize)
.Take(PageSize)
.Future<Organism>();
var result = query.ToList();
var rowcount = session.QueryOver<Organism>()
.Select(Projections.Count(Projections.Id()))
.FutureValue<int>().Value;
查詢對於相同組的數據作爲之前的,這是產生的查詢:
SELECT TOP (@p0) Id0_0_, Title0_0_ FROM (SELECT this_.Id as Id0_0_, this_.Title as Title0_0_, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM Organism this_) as query WHERE query.__hibernate_sort_row > @p1 ORDER BY query.__hibernate_sort_row;SELECT count(this_.Id) as y0_ FROM Organism this_;;@p0 = 10 [Type: Int32 (0)], @p1 = 10 [Type: Int32 (0)]
標準(與Future1分貝呼叫):
var criteria = session.CreateCriteria<Organism>()
.SetFirstResult((Page - 1) * PageSize)
.SetMaxResults(PageSize)
.Future<Organism>();
var countCriteria = session.CreateCriteria<Organism>()
.SetProjection(Projections.Count(Projections.Id()))
.FutureValue<int>().Value;
再次,查詢的相同的一組數據,標準與未來結果在同一查詢中:
SELECT TOP (@p0) Id0_0_, Title0_0_ FROM (SELECT this_.Id as Id0_0_, this_.Title as Title0_0_, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM Organism this_) as query WHERE query.__hibernate_sort_row > @p1 ORDER BY query.__hibernate_sort_row;SELECT count(this_.Id) as y0_ FROM Organism this_;;@p0 = 10 [Type: Int32 (0)], @p1 = 10 [Type: Int32 (0)]
請注意, e查詢樣式導致相同的查詢。未來的語法只允許NHibernate進行一次數據庫調用而不是兩次。
如果您使用的是NHibernate 3,我認爲最優雅的方式是使用新的QueryOver語法。 (你在你提出的解決方案中使用了舊的NHibernate.Linq語法,你最好學習QueryOver語法。)
可能值得注意的是,兩者是否導致單個查詢是由方言特定的優化驅動的。例如在SQLite上發出兩個查詢,但行爲是相同的(所有的期貨儘可能少地查詢) – AlexCuse