ASP.NET Core Web API Interview Question #117

What is keyset or cursor pagination?

Dependency Injection, Services, EF Core, Async & Data Access Mid-Level Intermediate

Quick Interview Answer

Keyset pagination requests records after a known ordered value instead of skipping an increasing number of rows. For example, the client may request products with Id greater than the last Id it received.

Detailed Explanation

Keyset pagination requests records after a known ordered value instead of skipping an increasing number of rows. For example, the client may request products with Id greater than the last Id it received.

It performs well for many large sequential result sets, but requires a stable ordering and does not naturally support jumping to an arbitrary page number.

Code Example

var items = await _db.Products
    .AsNoTracking()
    .Where(x => x.Id > lastId)
    .OrderBy(x => x.Id)
    .Take(pageSize)
    .ToListAsync(token);