2012-07-11 20 views
1

我有一個Repeater,我需要分頁,所以我使用PagedDatasource。Pageddatasource失敗分頁使用「不執行ICollection」。

考慮:

string tag = Request.QueryString["category"]; 

和標記變量不爲空,然後PagedDataSource失敗,出現以下錯誤分頁:

Exception Details: System.Web.HttpException: Cannot compute Count for a data source that does not implement ICollection.

在這一行:

lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + pagedDS.PageCount.ToString(); 

注意:當標籤變量是空的,然後分頁工作正常。

全部下面的代碼:

string tag = Request.QueryString["category"]; 
var ba = new DataBase.BlogAdapter(); 

PagedDataSource pagedDS = new PagedDataSource(); 
pagedDS.AllowPaging = true; 
pagedDS.PageSize = 3; 

if (String.IsNullOrEmpty(tag)) 
{ 
    pagedDS.DataSource = ba.GetArticles(null, null); 
} 
else 
{ 
    var t = new DataBase.Tag() { TagName = tag }; 
    var tec = new DataBase.TagEqualityComparer(); 
    pagedDS.DataSource = ba.GetArticles(null, null).Where(a => a.Tags.Contains(t, tec)); 
    CurrentPage = 0; 
} 

pagedDS.CurrentPageIndex = CurrentPage; 

// NEXT LINE FAILS IF "tag" IS NOT EMPTY 
lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + pagedDS.PageCount.ToString(); 

回答

2

你可能需要兌現的過濾器的集合,例如

ba.GetArticles(null, null).Where(a => a.Tags.Contains(t, tec)).ToList(); 

IQueryable<>只實現IEnumerable,而IList<>實現ICollection

+0

謝謝。它現在有效。我仍然不知道爲什麼它沒有過濾和過濾失敗 – levi 2012-07-11 08:28:13

+1

@levanlevi - 沒有過濾,你的ba.GetArticles()返回大概是一個具體的集合。但是,只要應用了* Where *,返回類型就是一個* IQueryable *,它尚未執行(即沒有ICollection屬性,如Count)。 PagedDataSource將不得不通過IEnumerable進行迭代來計算它,它不想做FWR。 – StuartLC 2012-07-11 08:34:43