PagedList using LINQ Skip and Take, but show paging using Count of results

前端 未结 5 1291
眼角桃花
眼角桃花 2020-12-15 06:02

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

5条回答
  •  误落风尘
    2020-12-15 06:24

    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.

提交回复
热议问题