PagedList and Async

人盡茶涼 提交于 2019-12-03 02:37:10

Is there a reasonable way to work with PagedList and async?

Yes. Doesn't come out of the box with EF. But, you can get PagedList.EntityFramework which exposes a ToPagedListAsync:

public class PagedListExtended<T> : BasePagedList<T> 
{
    private PagedListExtended() 
    {
    }

    public static async Task<IPagedList<T>> Create(IQueryable<T> superset, int pageNumber, int pageSize) 
    {
        var list = new PagedListExtended<T>();
        await list.Init(superset, pageNumber, pageSize);
        return list;
    }

    async Task Init(IQueryable<T> superset, int pageNumber, int pageSize)
    {
        if (pageNumber < 1)
              throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1.");
        if (pageSize < 1)
             throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1.");
        TotalItemCount = superset == null ? 0 : await superset.CountAsync();
        PageSize = pageSize;
        PageNumber = pageNumber;
        PageCount = TotalItemCount > 0 ? (int) Math.Ceiling(TotalItemCount/(double) PageSize) : 0;
        HasPreviousPage = PageNumber > 1;
        HasNextPage = PageNumber < PageCount;
        IsFirstPage = PageNumber == 1;
        IsLastPage = PageNumber >= PageCount;
        FirstItemOnPage = (PageNumber - 1)*PageSize + 1;
        var num = FirstItemOnPage + PageSize - 1;
        LastItemOnPage = num > TotalItemCount ? TotalItemCount : num;
        if (superset == null || TotalItemCount <= 0)
            return;
        Subset.AddRange(pageNumber == 1 ? await superset.Skip(0).Take(pageSize).ToListAsync() : await superset.Skip((pageNumber - 1)*pageSize).Take(pageSize).ToListAsync());
    }
}

public static class PagedListExtendedExtensions 
{
     public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int pageNumber, int pageSize)
     {
         return await PagedListExtended<T>.Create(superset, pageNumber, pageSize);
      }
}

Yes, you can.

You can use X.PagedList which is an improved version of PagedList and supports async operations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!