Search code examples
c#asp.netpaginationdatasourcerepeater

Pageddatasource fails paging With "does not implement ICollection."


I've a Repeater and I need paging, so I'm using PagedDatasource.

Given:

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

and tag variable is NOT empty then PagedDataSource fails paging with the following error:

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

at this line:

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

NOTE: When tag variable IS empty then paging works fine.

Full code below:

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(); 

Solution

  • You probably need to materialise the filter as a collection, e.g.

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

    IQueryable<> only implements IEnumerable, whereas IList<> implements ICollection.