
First we need to know about the traditional offset based pagination and the problems it introduces.
In the code below we are using the offset pagination.
[HttpGet("offset")]
public async Task<IActionResult> GetBooks(int limit=10, int page=1)
{
var books = await _context.Books
.AsNoTracking()
.OrderBy(a => a.Id)
.Skip(limit * (page - 1))
.Take(limit)
.ToListAsync();
return Ok(books);
}
Which translates to the following sql:
SELECT
[b].[Id],
[b].[Author],
[b].[Country],
[b].[ImageLink],
[b].[Language],
[b].[Link],
[b].[Pages],
[b].[Price],
[b].[Title],
[b].[Year]
FROM [Book] AS [b]
ORDER BY [b].[Id]
OFFSET @__p_0 ROWS
FETCH NEXT @__p_1 ROWS ONLY
Note: In every pagination logic, ordering must be unique. In our case we are using Id
which is unique.
[Read More]