简体   繁体   English

分页返回总计数和总页数

[英]Pagination to return total count and total pages

Problem 问题

I am creating an API and apply filter to it to paginate on the query. 我正在创建一个API并对其应用过滤器以对查询进行分页。 when send response now I need to send Totalcount and TotalPages with the result. 当发送响应时,我需要发送Totalcount和TotalPages以及结果。 by this method Now I am getting this kind of response in case of success 通过这种方法现在我得到了这种成功的回应

result 结果

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

Now I need this result instead of above result How can I achieve this need some good suggestion. 现在我需要这个结果而不是上面的结果我怎样才能实现这个需要一些好的建议。

Needed result 需要的结果

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

interface 接口

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

PageList Class PageList类

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

Service 服务

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

Solution

Just do this and my code woks as I expected 只要这样做,我的代码就像我预期的那样

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

because the properties of total pages and total count already in the IPagedList interface and it inherited in PagedList 因为总页数和总计数的属性已经在IPagedList接口中并且它继承在PagedList中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM