I wrote this pagination code:
[HttpGet("{page}")]
public async Task<ActionResult<List<Request>>> GetRequest(int page)
{
var pageResults = 10f;
var pageCount = Math.Ceiling(_context.Request.Count() / pageResults);
var requests = await _context.Request
.OrderByDescending(x => x.RequestDate)
.Skip((page - 1) * (int)pageResults)
.Take((int)pageResults)
.ToListAsync();
var response = new RequestResponse
{
Requests = requests,
CurrentPage = page,
Pages = (int)pageCount
};
return Ok(response);
}
Now, according to the official documentation of Microsoft: Pagination, this is a bad implementation, this method its gonna still calling all the data even if they aren't returned. As the user progresses further in the pagination, the timeouts will become longer because more data will have to be called.
If you read the docs you will see that the solution for this problem it's rather simple, however I don't quite get how I'm supposed to do that with my current implementation.
I tried the following, following the recommendation from the documentation:
[HttpGet("{page}")]
public async Task<ActionResult<List<Request>>> GetRequest(int page)
{
var pageResults = 10f;
// Added this variable
var lastDate = new DateTime(2020, 1, 1);
var pageCount = Math.Ceiling(_context.Request.Count() / pageResults);
var requests = await _context.Request
.OrderByDescending(x => x.RequestDate)
.Where(x => x.RequestDate > lastDate)
//.Skip((page - 1) * (int)pageResults)
.Take((int)pageResults)
.ToListAsync();
var response = new RequestResponse
{
Requests = requests,
CurrentPage = page,
Pages = (int)pageCount
};
return Ok(response);
}
This implementation always keeps returning the same page and I don't really get why.
Any idea how I can solve this?