2011-09-17 38 views
4

我有一個包含中繼器的用戶控件,它的datasouce是使用包含從代碼中的查詢返回的數據的IEnumerable對象設置的。中繼器具有分頁功能,併爲每個頁面顯示自定義數量的記錄。如何在IQueryable中使用Skip()和Take()

我不希望每次用戶單擊下一個按鈕時都加載所有數據,以查看中繼器中記錄的下一頁。我如何使它成爲IQueryable並使用Skip()和Take()僅顯示該頁面所需的記錄?

我有以下代碼:

//Code that assigns query to repeater data source 
DataSet = QueryGoesHere.ToArray(); // returns IEnumerable 
repeater.DataSource = DataSet; 
repeater.DataBind(); 

//Code that creates PagedDataSource - how can I update this to make it display only the records that are needed for the currently displayed page? 

      objPds = new PagedDataSource(); 
      objPds.DataSource = DataSource 
      objPds.AllowPaging = true; 
      objPds.PageSize = 5; 
      objPds.CurrentPageIndex = CurrentPage; 
      lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objPds.PageCount.ToString(); 

回答

4

,如果我得到你的權利,你要全部使用加載自己的實現istead數據,然後使用PagedDataSource的權利?

如果是這樣,你必須確保QueryGoesHere是一個Queryable支持此(Linq2Sql或EF)。然後,你必須讓你的約會像這樣的

var count = QueryGoesHere.Count(); 

計,讓你想顯示的數據部分:

var skip = (curPageNumber - 1)*itemsPerPage; 
var display = Math.Min(count - skip, itemsPerPage); 

,只是使用

var displayedItems = QueryGoesHere.Skip(skip).Take(display).ToArray(); 

這應該做訣竅。

1
public static Dictionary<string, string> SampleDataList(int startIndex, int pageSize) 
    { 
     Dictionary<string, string> sampleTable = new Dictionary<string, string>(); 
     var query = from p in TemporaryData() 
        .Take(pageSize) 
        .Skip(startIndex) 
        select new 
        { 
         FirstColumn = p.Key, 
         SecondColumn = p.Value 
        }; 
     foreach (var row in query) 
     { 
      sampleTable.Add(row.FirstColumn, row.SecondColumn); 
     } 
     return sampleTable; 
    } 

下面的鏈接將幫助您瞭解如何使用分頁與中繼

http://cmsnsoftware.blogspot.com/2011/07/how-to-use-custom-pagination.html

相關問題