Entity Framework Core Interview Question #60
What is keyset pagination and when is it useful?
LINQ Queries, Filtering, Projection & Query Execution Mid-Level Intermediate
Quick Interview Answer
Keyset, or seek, pagination retrieves the next page relative to the last seen ordering key instead of skipping an increasing number of rows.
Detailed Explanation
Keyset, or seek, pagination retrieves the next page relative to the last seen ordering key instead of skipping an increasing number of rows.
For large datasets and next/previous navigation, it can be more efficient and stable than deep offset pagination. The ordering keys should be deterministic and supported by appropriate indexes.
It is less convenient when users must jump directly to an arbitrary page number.
Code Example
var page = await db.Products
.Where(p => p.Id > lastSeenId)
.OrderBy(p => p.Id)
.Take(25)
.ToListAsync();