分页返回总计数和总页数

时间:2017-08-23 12:53:04

标签: c# asp.net-core pagination entity-framework-core

问题

我正在创建一个API并对其应用过滤器以对查询进行分页。当发送响应时,我需要发送Totalcount和TotalPages以及结果。通过这种方法现在我得到了这种成功的回应

结果

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 

现在我需要这个结果而不是上面的结果我怎样才能达到这个需要一些好的建议。

需要的结果

sudo service mysql restart

接口

{
  "error": "string",
  "success": "string",
  "statusCode": 0,
  "result": {
      [ {object}, ]
   }
}

PageList类

{
  "error": "string",
  "success": "string",
  "statusCode": 0,
  "result": {
      "TotalPages":"50",
      "TotalCount":"908945",
      "model":[ {object},]
   }
}

服务

public interface IPagedList<T> : IList<T>
{
    int PageIndex { get; }
    int PageSize { get; }
    int TotalCount { get; }
    int TotalPages { get; }
    bool HasPreviousPage { get; }
    bool HasNextPage { get; }
}

API

public class PagedList<T> : List<T>, IPagedList<T>
{

    public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
    {
        int total = source.Count();
        this.TotalCount = total;
        this.TotalPages = total / pageSize;

        if (total % pageSize > 0)
            TotalPages++;

        this.PageSize = pageSize;
        this.PageIndex = pageIndex;
        this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
    }


    public PagedList(IList<T> source, int pageIndex, int pageSize)
    {
        TotalCount = source.Count();
        TotalPages = TotalCount / pageSize;

        if (TotalCount % pageSize > 0)
            TotalPages++;

        this.PageSize = pageSize;
        this.PageIndex = pageIndex;
        this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
    }


    public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
    {
        TotalCount = totalCount;
        TotalPages = TotalCount / pageSize;

        if (TotalCount % pageSize > 0)
            TotalPages++;

        this.PageSize = pageSize;
        this.PageIndex = pageIndex;
        this.AddRange(source);
    }

    public int PageIndex { get; private set; }
    public int PageSize { get; private set; }
    public int TotalCount { get; private set; }
    public int TotalPages { get; private set; }

    public bool HasPreviousPage
    {
        get { return (PageIndex > 0); }
    }
    public bool HasNextPage
    {
        get { return (PageIndex + 1 < TotalPages); }
    }
}

ApiResponse

public IPagedList<Model> SomeMethod(int id, int pageIndex = 0, int pageSize = int.MaxValue)
{
    var query = //select query

    var reslt= new PagedList<Model>(query, pageIndex, pageSize);
    return reslt;


}

1 个答案:

答案 0 :(得分:2)

解决方案

就这样做,我的代码按照我的预期进行了调整

var TotalCount =result.TotalCount
var TotalPages =result.TotalPages
//after getting these value pass in the result
return new ApiResponse(StatusCodes.Status200OK, result: new {result : result, totalCount : TotalCount, totalPages : TotalPages}, "success"); 

因为总页数和总计数的属性已经在IPagedList接口中并且它在PagedList中继承了