Pagination in EF Core
Pagination allows you to split large result sets into smaller chunks, making it easier to display data in UI components.
Key Topics
Basic Pagination
Example
using System;
using System.Linq;
class Program
{
static void Main()
{
int pageSize = 5;
int pageNumber = 2;
using (var context = new MyDbContext())
{
var employees = context.Employees
.OrderBy(e => e.Id)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
foreach (var employee in employees)
{
Console.WriteLine("Name: " + employee.Name);
}
}
}
}
Explanation: This example uses Skip()
and Take()
to implement pagination for retrieving a specific page of records.
Key Takeaways
- Use
Skip()
to bypass records andTake()
to fetch a limited number of records. - Combine pagination with sorting for consistent results.