【问题标题】:Pagination to return total count and total pages分页返回总计数和总页数
【发布时间】:2018-02-01 01:52:54
【问题描述】:

问题

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

结果

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

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

需要的结果

{
  "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; }
}

页面列表类

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

服务

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;


}

API

[HttpGet("{id}")]
public ApiResponse GetApi([FromRoute] int id, int pageIndex = 0, int pageSize = int.MaxValue)
{
    if (id == 0)
        return new ApiResponse(StatusCodes.Status400BadRequest, error: "id must be provided");


    var result = someMethod(id, pageIndex, pageSize);
    return new ApiResponse(StatusCodes.Status200OK, result, "success");
}

ApiResponse

public class ApiResponse
{

    public ApiResponse(int statusCode, object result = null, string success = "", string error = "")
    {
        Error = error;
        Success = success;
        StatusCode = statusCode;
        Result = result;      
    }

    public string Error { get; set; }
    public string Success { get; set; }
    public int StatusCode { get; set; }
    public Object Result { get; set; }
}

【问题讨论】:

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


【解决方案1】:

解决方案

只要这样做,我的代码就会像我预期的那样工作

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中

【讨论】:

  • 2 个小问题。 1) ='s not :'s 2) 结果属性应该是模型。新 { 模型 = 结果,totalCount = TotalCount,totalPages = totalPages }
  • 并且 'result' 将是响应对象中的 'data' 除非您将 ApiResponse.Data 重命名为 ApiResponse.Result { "statusCode": "200", "data": { "model": [ { "myProperty": "Hello World" } ], "totalCount": 908945, "totalPages": 50 }, "success": "success", "error": null }
  • 每次都回答自己的问题?假设这是“建立你的代表”的一种方式。
  • 回答你自己的问题完全没问题。甚至 SO 也建议这样做:stackoverflow.blog/2011/07/01/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 2018-02-16
  • 2021-11-16
  • 1970-01-01
  • 2017-04-04
相关资源
最近更新 更多