Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 10610 | Bulk Product e3b19b7f | Sports & Outdoors | $822.33 | 292 | ★ 0.4 | 59 | BULK-c5757fa1 |
| 10611 | Bulk Product 5152eb15 | Electronics | $499.79 | 252 | ★ 4.5 | 52 | BULK-aa5be526 |
| 10612 | Bulk Product cedb7821 | Books | $235.78 | 172 | ★ 1.0 | 12 | BULK-56f36b40 |
| 10613 | Bulk Product d3d7b8b0 | Clothing | $600.15 | 488 | ★ 4.2 | 60 | BULK-8a804f11 |
| 10614 | Bulk Product f02c21ea | Clothing | $557.50 | 245 | ★ 2.0 | 77 | BULK-f0877610 |
| 10615 | Bulk Product 645f92b9 | Clothing | $861.01 | 449 | ★ 1.2 | 47 | BULK-9ec626e7 |
| 10616 | Bulk Product 792e7d3f | Books | $151.20 | 482 | ★ 4.6 | 73 | BULK-99f32d50 |
| 10617 | Bulk Product 185d77b7 | Home & Garden | $943.46 | 322 | ★ 4.7 | 38 | BULK-48bb10e7 |
| 10618 | Bulk Product b5ad2791 | Sports & Outdoors | $928.22 | 95 | ★ 3.7 | 41 | BULK-9d549789 |
| 10619 | Bulk Product 1681970e | Clothing | $835.36 | 195 | ★ 1.3 | 18 | BULK-57450f54 |
| 10620 | Bulk Product d7516f0e | Books | $190.73 | 76 | ★ 3.2 | 28 | BULK-62be428a |
| 10621 | Bulk Product 10a1a212 | Clothing | $139.31 | 38 | ★ 4.2 | 41 | BULK-f15f7310 |
| 10622 | Bulk Product e60a5617 | Sports & Outdoors | $661.83 | 465 | ★ 2.0 | 12 | BULK-80de1c05 |
| 10623 | Bulk Product d0993b5b | Home & Garden | $210.94 | 269 | ★ 3.6 | 25 | BULK-0c618c7a |
| 10624 | Bulk Product 57c26f9d | Home & Garden | $229.87 | 446 | ★ 4.5 | 12 | BULK-4c755887 |
| 10625 | Bulk Product fa430dbf | Toys & Games | $494.79 | 61 | ★ 4.8 | 88 | BULK-a2487f3a |
| 10626 | Bulk Product 2ce98ba7 | Sports & Outdoors | $162.51 | 261 | ★ 1.7 | 21 | BULK-f40e1e9d |
| 10627 | Bulk Product 967421a2 | Electronics | $606.86 | 65 | ★ 1.0 | 22 | BULK-366d2e2b |
| 10628 | Bulk Product 4eb10d97 | Clothing | $497.34 | 262 | ★ 1.9 | 62 | BULK-f08aabfe |
| 10629 | Bulk Product e6ef7234 | Electronics | $785.48 | 186 | ★ 3.3 | 11 | BULK-8e011491 |
| 10630 | Bulk Product acae4f0f | Electronics | $569.32 | 61 | ★ 3.1 | 64 | BULK-2c60b198 |
| 10631 | Bulk Product 48497da7 | Electronics | $529.53 | 417 | ★ 4.7 | 27 | BULK-6db34cba |
| 10632 | Bulk Product 1f80ea8a | Electronics | $1,005.52 | 490 | ★ 4.4 | 82 | BULK-9fab8fe4 |
| 10633 | Bulk Product 5a79f95c | Books | $520.04 | 440 | ★ 2.8 | 8 | BULK-f729ed34 |
| 10634 | Bulk Product 0f609932 | Sports & Outdoors | $81.15 | 75 | ★ 4.0 | 56 | BULK-6d07133e |
// Efficient pagination with Skip and Take
public async Task<PaginatedResult<DemoProduct>> GetPaginatedProductsAsync(int page, int pageSize)
{
// Get total count (cached if possible)
var totalCount = await _context.DemoProducts.CountAsync(p => p.IsActive);
// Get page of results
var items = await _context.DemoProducts
.Where(p => p.IsActive)
.OrderBy(p => p.Id) // Important: Must have ORDER BY for consistent paging
.Skip((page - 1) * pageSize) // OFFSET in SQL
.Take(pageSize) // FETCH NEXT in SQL
.AsNoTracking() // No tracking needed for read-only
.ToListAsync();
return new PaginatedResult<DemoProduct>
{
Items = items,
TotalCount = totalCount,
Page = page,
PageSize = pageSize
};
}