2008-09-10 52 views

回答

107

ICriteria有一個SetFirstResult(int i)方法,它表示您希望獲得的第一個項目(基本上是您的頁面中的第一個數據行)的索引。

它也有一個SetMaxResults(int i)方法,這表明你希望得到(即頁面大小)的行數。

例如,這個標準對象都將獲得前10個結果數據網格:

criteria.SetFirstResult(0).SetMaxResults(10); 
+1

這幾乎是Linq(至NH)語法看起來總是 - 尼斯。 – MotoWilliams 2008-09-17 05:04:15

+13

重要的是要注意,您將需要執行單獨的事務來檢索總行數以呈現您的尋呼機。 – 2009-01-07 00:44:45

23

如何通過Ayende在this blog post討論使用LINQ to NHibernate的?

代碼示例:

(from c in nwnd.Customers select c.CustomerID) 
     .Skip(10).Take(10).ToList(); 

這裏是一個詳細的職位由NHibernate的團隊博客上Data Access With NHibernate包括實現分頁。

+0

注意的LINQ to NHibernate的是在contrib包並沒有包含在NHibernate的2.0版本 – Richard 2008-09-23 08:54:36

6

我建議你創建一個專門的機構來處理分頁。喜歡的東西(我是一個Java程序員,但應該很容易地圖):

public class Page { 

    private List results; 
    private int pageSize; 
    private int page; 

    public Page(Query query, int page, int pageSize) { 

     this.page = page; 
     this.pageSize = pageSize; 
     results = query.setFirstResult(page * pageSize) 
      .setMaxResults(pageSize+1) 
      .list(); 

    } 

    public List getNextPage() 

    public List getPreviousPage() 

    public int getPageCount() 

    public int getCurrentPage() 

    public void setPageSize() 

} 

我沒有提供一個實現,但你可以使用由@Jon建議的方法。這裏有一個good discussion供您參考。

11

最有可能在GridView中,您將希望顯示一段數據加上與查詢匹配的數據總量的總行數(rowcount)。

您應該使用MultiQuery在一次調用中將Select count(*)查詢和.SetFirstResult(n).SetMaxResult(m)查詢發送到數據庫。

注意結果將是一個列表,其中包含2個列表,一個用於數據切片,另一個用於計數。

例子:

IMultiQuery multiQuery = s.CreateMultiQuery() 
    .Add(s.CreateQuery("from Item i where i.Id > ?") 
      .SetInt32(0, 50).SetFirstResult(10)) 
    .Add(s.CreateQuery("select count(*) from Item i where i.Id > ?") 
      .SetInt32(0, 50)); 
IList results = multiQuery.List(); 
IList items = (IList)results[0]; 
long count = (long)((IList)results[1])[0]; 
31
public IList<Customer> GetPagedData(int page, int pageSize, out long count) 
     { 
      try 
      { 
       var all = new List<Customer>(); 

       ISession s = NHibernateHttpModule.CurrentSession; 
       IList results = s.CreateMultiCriteria() 
            .Add(s.CreateCriteria(typeof(Customer)).SetFirstResult(page * pageSize).SetMaxResults(pageSize)) 
            .Add(s.CreateCriteria(typeof(Customer)).SetProjection(Projections.RowCountInt64())) 
            .List(); 

       foreach (var o in (IList)results[0]) 
        all.Add((Customer)o); 

       count = (long)((IList)results[1])[0]; 
       return all; 
      } 
      catch (Exception ex) { throw new Exception("GetPagedData Customer da hata", ex); } 
     } 

當尋呼數據是那裏得到的多標準輸入結果的另一種方式或者每個人都做了同樣的我一樣?

感謝

87

您也可以利用期貨的NHibernate的功能來執行查詢,以獲得總記錄數以及在單個查詢的實際效果。

// Get the total row count in the database. 
var rowCount = this.Session.CreateCriteria(typeof(EventLogEntry)) 
    .Add(Expression.Between("Timestamp", startDate, endDate)) 
    .SetProjection(Projections.RowCount()).FutureValue<Int32>(); 

// Get the actual log entries, respecting the paging. 
var results = this.Session.CreateCriteria(typeof(EventLogEntry)) 
    .Add(Expression.Between("Timestamp", startDate, endDate)) 
    .SetFirstResult(pageIndex * pageSize) 
    .SetMaxResults(pageSize) 
    .Future<EventLogEntry>(); 

要獲得總記錄數,請執行以下操作:

int iRowCount = rowCount.Value; 

什麼期貨給你的是here了很好的討論。

39

在NHibernate的3你可以使用QueryOver

var pageRecords = nhSession.QueryOver<TEntity>() 
      .Skip(PageNumber * PageSize) 
      .Take(PageSize) 
      .List(); 

你也可以明確地訂購您的結果是這樣的:

var pageRecords = nhSession.QueryOver<TEntity>() 
      .OrderBy(t => t.AnOrderFieldLikeDate).Desc 
      .Skip(PageNumber * PageSize) 
      .Take(PageSize) 
      .List();