Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 14385 | Bulk Product 0a870323 | Sports & Outdoors | $689.96 | 485 | ★ 2.9 | 28 | BULK-c1ff9dec |
| 14386 | Bulk Product 09a458e2 | Books | $794.28 | 25 | ★ 4.3 | 42 | BULK-d2bbff51 |
| 14387 | Bulk Product a6572500 | Clothing | $797.58 | 193 | ★ 3.5 | 81 | BULK-119f8404 |
| 14388 | Bulk Product 4218fd03 | Clothing | $110.40 | 492 | ★ 1.7 | 85 | BULK-f03291be |
| 14389 | Bulk Product 86154528 | Sports & Outdoors | $92.47 | 103 | ★ 0.9 | 29 | BULK-506bc654 |
| 14390 | Bulk Product 5c6f07eb | Books | $65.79 | 342 | ★ 3.2 | 49 | BULK-63b4f9e9 |
| 14391 | Bulk Product d1d0d257 | Books | $579.13 | 45 | ★ 3.0 | 41 | BULK-eb00281e |
| 14392 | Bulk Product 924dcf9b | Sports & Outdoors | $97.26 | 4 | ★ 0.4 | 48 | BULK-19d18a6c |
| 14393 | Bulk Product 30876789 | Books | $461.54 | 13 | ★ 3.1 | 12 | BULK-f47bb154 |
| 14394 | Bulk Product c33859ea | Books | $219.79 | 233 | ★ 4.8 | 24 | BULK-d6469058 |
| 14395 | Bulk Product 10658ad2 | Electronics | $779.89 | 479 | ★ 4.6 | 52 | BULK-bd0223a0 |
| 14396 | Bulk Product ffa6cfea | Home & Garden | $372.24 | 455 | ★ 2.3 | 15 | BULK-2de811db |
| 14397 | Bulk Product f9c65c8a | Electronics | $208.31 | 359 | ★ 3.7 | 33 | BULK-3cbd5265 |
| 14398 | Bulk Product 5916d66e | Toys & Games | $363.09 | 105 | ★ 0.7 | 92 | BULK-d9e56644 |
| 14399 | Bulk Product 81c7dd16 | Books | $69.75 | 292 | ★ 4.7 | 39 | BULK-08dbcf61 |
| 14400 | Bulk Product dde65b00 | Books | $929.01 | 387 | ★ 2.2 | 53 | BULK-dacd35e9 |
| 14401 | Bulk Product 9c1493b6 | Home & Garden | $558.29 | 485 | ★ 3.8 | 86 | BULK-f07481e7 |
| 14402 | Bulk Product c7490fd5 | Electronics | $124.76 | 477 | ★ 2.7 | 15 | BULK-379b0311 |
| 14403 | Bulk Product aa4a4d01 | Home & Garden | $885.68 | 219 | ★ 3.3 | 3 | BULK-e001d043 |
| 14404 | Bulk Product 9fd08d87 | Clothing | $400.05 | 494 | ★ 3.2 | 51 | BULK-5c35d115 |
| 14405 | Bulk Product bacd2ff0 | Toys & Games | $364.35 | 448 | ★ 0.5 | 76 | BULK-7cde4fc6 |
| 14406 | Bulk Product ce49a8d4 | Electronics | $43.66 | 114 | ★ 1.7 | 82 | BULK-8da2501d |
| 14407 | Bulk Product 6c607990 | Sports & Outdoors | $193.77 | 407 | ★ 1.5 | 76 | BULK-c16525b5 |
| 14408 | Bulk Product 391f166d | Toys & Games | $426.50 | 73 | ★ 1.5 | 50 | BULK-1e112b67 |
| 14409 | Bulk Product 3ac58937 | Books | $894.57 | 475 | ★ 3.6 | 4 | BULK-97b89671 |
// 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
};
}