Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 17210 | Bulk Product 14252cad | Sports & Outdoors | $389.47 | 234 | ★ 1.7 | 35 | BULK-5f962950 |
| 17211 | Bulk Product a1b87c7c | Clothing | $73.59 | 339 | ★ 1.2 | 20 | BULK-cc26e3ea |
| 17212 | Bulk Product 831458c7 | Books | $54.70 | 498 | ★ 4.3 | 73 | BULK-9208650b |
| 17213 | Bulk Product a7357540 | Home & Garden | $38.16 | 384 | ★ 0.4 | 16 | BULK-05f09b70 |
| 17214 | Bulk Product 3d6c6b15 | Home & Garden | $129.26 | 152 | ★ 1.3 | 96 | BULK-cf139391 |
| 17215 | Bulk Product eb196320 | Home & Garden | $560.57 | 72 | ★ 3.2 | 7 | BULK-601082a6 |
| 17216 | Bulk Product b8f70fbd | Sports & Outdoors | $276.01 | 301 | ★ 1.3 | 80 | BULK-ef51b635 |
| 17217 | Bulk Product a9cd0dfd | Home & Garden | $801.70 | 37 | ★ 1.6 | 68 | BULK-41a68c44 |
| 17218 | Bulk Product 2e568ef0 | Home & Garden | $534.59 | 19 | ★ 1.6 | 8 | BULK-5138ce8c |
| 17219 | Bulk Product ca459994 | Books | $282.73 | 387 | ★ 4.6 | 94 | BULK-64ccd24c |
| 17220 | Bulk Product 9da4452e | Electronics | $727.73 | 403 | ★ 2.5 | 89 | BULK-f708c068 |
| 17221 | Bulk Product 284e5889 | Clothing | $188.30 | 18 | ★ 4.1 | 43 | BULK-facd5646 |
| 17222 | Bulk Product 9d775997 | Books | $23.38 | 325 | ★ 4.2 | 41 | BULK-6db23dae |
| 17223 | Bulk Product 67debe50 | Home & Garden | $733.34 | 5 | ★ 4.5 | 29 | BULK-4e249189 |
| 17224 | Bulk Product b266395b | Electronics | $588.28 | 319 | ★ 0.6 | 7 | BULK-b39cc050 |
| 17225 | Bulk Product fb7e9e30 | Home & Garden | $854.54 | 238 | ★ 3.9 | 75 | BULK-60df432a |
| 17226 | Bulk Product f300d140 | Books | $534.82 | 382 | ★ 3.0 | 77 | BULK-4650ef51 |
| 17227 | Bulk Product e3180ae9 | Toys & Games | $807.66 | 111 | ★ 4.6 | 86 | BULK-7612b5cf |
| 17228 | Bulk Product 0c239644 | Clothing | $106.71 | 4 | ★ 4.8 | 14 | BULK-044d061f |
| 17229 | Bulk Product 76b7258a | Sports & Outdoors | $304.05 | 0 | ★ 1.3 | 34 | BULK-e73745b1 |
| 17230 | Bulk Product 0062db01 | Toys & Games | $771.93 | 336 | ★ 3.6 | 81 | BULK-ea5e4e3c |
| 17231 | Bulk Product 08a91463 | Electronics | $812.34 | 266 | ★ 2.3 | 60 | BULK-4447b879 |
| 17232 | Bulk Product 9df7fe17 | Home & Garden | $756.51 | 183 | ★ 2.1 | 87 | BULK-5f4a40e7 |
| 17233 | Bulk Product 6f92cee2 | Toys & Games | $182.33 | 469 | ★ 4.3 | 46 | BULK-6616a17c |
| 17234 | Bulk Product f9aaee99 | Home & Garden | $12.23 | 428 | ★ 3.0 | 27 | BULK-de629e4f |
// 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
};
}