Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 18310 | Bulk Product 45b24b97 | Clothing | $877.09 | 44 | ★ 4.5 | 13 | BULK-826418be |
| 18311 | Bulk Product 8242a9f1 | Home & Garden | $134.33 | 313 | ★ 1.3 | 72 | BULK-e734bef1 |
| 18312 | Bulk Product 975f33fc | Clothing | $970.21 | 186 | ★ 4.8 | 81 | BULK-e270baf1 |
| 18313 | Bulk Product 88ea75ac | Electronics | $839.42 | 73 | ★ 2.8 | 83 | BULK-0edd9c53 |
| 18314 | Bulk Product dd6f1077 | Sports & Outdoors | $374.32 | 253 | ★ 1.3 | 37 | BULK-b27fcc5b |
| 18315 | Bulk Product 1f3c0451 | Books | $1,002.85 | 192 | ★ 4.3 | 22 | BULK-8ef05f91 |
| 18316 | Bulk Product d1f2ad8b | Sports & Outdoors | $344.67 | 116 | ★ 4.4 | 35 | BULK-eb8c8160 |
| 18317 | Bulk Product 6d05b523 | Clothing | $380.20 | 400 | ★ 4.3 | 95 | BULK-7bcf7318 |
| 18318 | Bulk Product 5c010863 | Sports & Outdoors | $507.24 | 182 | ★ 3.2 | 90 | BULK-de522433 |
| 18319 | Bulk Product 7c24f506 | Sports & Outdoors | $335.53 | 57 | ★ 4.3 | 81 | BULK-74da9810 |
| 18320 | Bulk Product 6bace188 | Clothing | $639.64 | 27 | ★ 1.8 | 85 | BULK-21b8bbfa |
| 18321 | Bulk Product a2dca56d | Sports & Outdoors | $281.79 | 465 | ★ 3.3 | 68 | BULK-8d578f39 |
| 18322 | Bulk Product 96758fd5 | Home & Garden | $313.28 | 278 | ★ 1.5 | 56 | BULK-0daa8757 |
| 18323 | Bulk Product 084a2f85 | Sports & Outdoors | $725.65 | 441 | ★ 0.8 | 79 | BULK-122a7feb |
| 18324 | Bulk Product 4ffbeec1 | Home & Garden | $284.42 | 208 | ★ 3.5 | 29 | BULK-c43c0635 |
| 18325 | Bulk Product 237b4d34 | Books | $398.25 | 438 | ★ 2.5 | 11 | BULK-8f9d001d |
| 18326 | Bulk Product 47e4cf01 | Clothing | $791.85 | 70 | ★ 0.8 | 92 | BULK-f07c1fb6 |
| 18327 | Bulk Product eec6427a | Sports & Outdoors | $318.24 | 348 | ★ 4.9 | 29 | BULK-8941f9b8 |
| 18328 | Bulk Product df203639 | Clothing | $929.86 | 66 | ★ 0.6 | 31 | BULK-5bcdf2b0 |
| 18329 | Bulk Product 21e30b06 | Clothing | $979.10 | 479 | ★ 3.8 | 95 | BULK-c80acfde |
| 18330 | Bulk Product ced8ff05 | Home & Garden | $200.63 | 323 | ★ 0.7 | 41 | BULK-69c703d2 |
| 18331 | Bulk Product 5cb0c84c | Sports & Outdoors | $428.53 | 173 | ★ 4.0 | 5 | BULK-dbfaa50e |
| 18332 | Bulk Product e7af4085 | Electronics | $786.41 | 233 | ★ 0.5 | 75 | BULK-73cc3503 |
| 18333 | Bulk Product 63ea4492 | Sports & Outdoors | $108.35 | 453 | ★ 2.5 | 22 | BULK-cc374ef3 |
| 18334 | Bulk Product fb2b8dfa | Sports & Outdoors | $460.28 | 394 | ★ 0.0 | 74 | BULK-fc73d8ca |
// 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
};
}