Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 20410 | Bulk Product 3cd04d11 | Electronics | $460.96 | 280 | ★ 2.1 | 41 | BULK-c52cbeda |
| 20411 | Bulk Product 91b07ef4 | Toys & Games | $371.11 | 79 | ★ 3.8 | 19 | BULK-30242a03 |
| 20412 | Bulk Product c7bc9bb5 | Sports & Outdoors | $806.28 | 298 | ★ 1.1 | 21 | BULK-af5cf4df |
| 20413 | Bulk Product d981df1d | Home & Garden | $320.24 | 94 | ★ 1.7 | 77 | BULK-420ebc3e |
| 20414 | Bulk Product ef64bd5c | Electronics | $462.72 | 164 | ★ 2.9 | 40 | BULK-9d977044 |
| 20415 | Bulk Product 9a71b7c3 | Sports & Outdoors | $843.34 | 270 | ★ 2.1 | 70 | BULK-7651cf6c |
| 20416 | Bulk Product 17b10565 | Books | $173.64 | 364 | ★ 3.0 | 57 | BULK-29a13cc0 |
| 20417 | Bulk Product 9739a8bd | Clothing | $138.34 | 96 | ★ 2.0 | 19 | BULK-0e26d4a3 |
| 20418 | Bulk Product 2f8f01b8 | Electronics | $328.63 | 134 | ★ 3.4 | 67 | BULK-e6f9d1a0 |
| 20419 | Bulk Product 48807c0c | Sports & Outdoors | $792.56 | 486 | ★ 3.6 | 63 | BULK-47acb340 |
| 20420 | Bulk Product 2c67f1c4 | Electronics | $421.61 | 51 | ★ 1.1 | 20 | BULK-9a740dbb |
| 20421 | Bulk Product 025d65db | Home & Garden | $739.69 | 488 | ★ 4.5 | 57 | BULK-b354a94a |
| 20422 | Bulk Product c48dadac | Home & Garden | $168.24 | 92 | ★ 1.3 | 60 | BULK-7947d36f |
| 20423 | Bulk Product fbe602b4 | Clothing | $498.10 | 128 | ★ 2.0 | 7 | BULK-82b2da4c |
| 20424 | Bulk Product e49c3325 | Clothing | $33.43 | 129 | ★ 4.7 | 44 | BULK-87e1d4bb |
| 20425 | Bulk Product f8581c4f | Toys & Games | $400.92 | 394 | ★ 3.5 | 82 | BULK-7e7f4fd7 |
| 20426 | Bulk Product d44418ec | Books | $65.03 | 328 | ★ 4.6 | 58 | BULK-b7e89af1 |
| 20427 | Bulk Product 9eb8e635 | Sports & Outdoors | $83.51 | 265 | ★ 0.6 | 95 | BULK-5ed9638a |
| 20428 | Bulk Product 45d25592 | Books | $875.05 | 150 | ★ 4.9 | 67 | BULK-5f07101d |
| 20429 | Bulk Product 1ce10e7a | Books | $493.57 | 118 | ★ 0.0 | 41 | BULK-dafc3ecd |
| 20430 | Bulk Product 450a2353 | Electronics | $344.11 | 348 | ★ 3.0 | 90 | BULK-2db5efeb |
| 20431 | Bulk Product dfeadfd1 | Clothing | $422.20 | 352 | ★ 1.5 | 32 | BULK-cb5d620a |
| 20432 | Bulk Product 8be62d66 | Clothing | $983.86 | 478 | ★ 3.9 | 70 | BULK-27ea70bf |
| 20433 | Bulk Product efffa0d7 | Electronics | $629.94 | 43 | ★ 1.8 | 99 | BULK-2d0b213d |
| 20434 | Bulk Product 6221c24d | Books | $660.07 | 382 | ★ 4.1 | 36 | BULK-aa28dfd5 |
// 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
};
}