ASP.NET Core MVC Interview Question #148
How should pagination be implemented for large MVC result sets?
Dependency Injection, Services, Error Handling, Logging & Performance Senior Advanced
Quick Interview Answer
Large result sets should be paginated at the data source rather than loading every record and then displaying only one page. A query commonly applies a deterministic OrderBy followed by Skip and Take, or uses keyset pagination for suitable high-scale scenarios.
Detailed Explanation
Large result sets should be paginated at the data source rather than loading every record and then displaying only one page. A query commonly applies a deterministic OrderBy followed by Skip and Take, or uses keyset pagination for suitable high-scale scenarios.
The ViewModel should include the current page and enough metadata to generate navigation. Always validate page-size limits so a client cannot request an unbounded amount of data.
Code Example
const int pageSize = 20;
var items = await query
.OrderBy(p => p.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync(token);