I am trying to display a filtered list of of products, based on Category filter and ItemsPerPage but I\'m having some issues when trying to use it with PagedList.
So
ToPagedList uses Take and Skip internally and when you use the extension to the IQueryable class, that will result in the database query you require. It also retrieves the TotalItemCount into its Metadata.
Sometimes you may need to bring the query results into memory because you are using a method that cannot be translated into sql. That means you have to convert the PagedList back to an Enumerable. You can get around this problem by using the StaticPagedList method like this:
var superset= repository.Products
.Where(p => this.CurrentCategory == null
|| p.Category == this.CurrentCategory)
.OrderBy(p => p.ProductID)
.ToPagedList(page, PageSize);
var subset = superset
.AsEnumerable()
.Select(p => new ProductViewModel
{
OtherData = p.UntranslateableMethod()
})
var model = new StaticPagedList(subset,
superset.GetMetaData());
}
From the summary of the method in comments:
Initializes a new instance of the PagedList.StaticPagedList class that contains the already divided subset and information about the size of the superset and the subset's position within it.