Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 13510 | Bulk Product 155a3c56 | Electronics | $655.60 | 236 | ★ 0.5 | 16 | BULK-21b1fefd |
| 13511 | Bulk Product 05de3ebb | Toys & Games | $486.30 | 334 | ★ 4.5 | 78 | BULK-b2593cbc |
| 13512 | Bulk Product 11a86ff0 | Home & Garden | $552.45 | 65 | ★ 2.5 | 84 | BULK-169f8c97 |
| 13513 | Bulk Product a41806e1 | Books | $623.63 | 158 | ★ 2.8 | 9 | BULK-dac8cc0c |
| 13514 | Bulk Product 84de7d9a | Sports & Outdoors | $89.60 | 106 | ★ 4.9 | 38 | BULK-dc02dc78 |
| 13515 | Bulk Product 7b71b094 | Electronics | $623.23 | 405 | ★ 1.9 | 39 | BULK-4deeac39 |
| 13516 | Bulk Product 1d269207 | Home & Garden | $286.38 | 363 | ★ 1.5 | 39 | BULK-d82e3a74 |
| 13517 | Bulk Product 6bb4d065 | Home & Garden | $992.35 | 264 | ★ 2.7 | 97 | BULK-89215ab8 |
| 13518 | Bulk Product 54bf21b5 | Sports & Outdoors | $917.23 | 133 | ★ 0.4 | 83 | BULK-5ce3bff7 |
| 13519 | Bulk Product c49a252d | Electronics | $245.76 | 229 | ★ 3.4 | 69 | BULK-05c61326 |
| 13520 | Bulk Product 65170b11 | Clothing | $764.89 | 349 | ★ 0.1 | 77 | BULK-c1274d0f |
| 13521 | Bulk Product 4f700491 | Books | $906.88 | 339 | ★ 2.2 | 85 | BULK-93becbac |
| 13522 | Bulk Product ce1eb9ff | Books | $318.61 | 412 | ★ 2.5 | 15 | BULK-bfadc5a1 |
| 13523 | Bulk Product ce825e4a | Books | $826.31 | 197 | ★ 1.6 | 90 | BULK-a8b77d2b |
| 13524 | Bulk Product 02165214 | Sports & Outdoors | $289.65 | 131 | ★ 1.7 | 60 | BULK-81379b8c |
| 13525 | Bulk Product 8b633605 | Books | $791.69 | 245 | ★ 1.1 | 48 | BULK-b280eb7f |
| 13526 | Bulk Product a9f2611a | Toys & Games | $216.24 | 266 | ★ 2.5 | 58 | BULK-9f5e0c54 |
| 13527 | Bulk Product f01a588c | Books | $571.05 | 112 | ★ 3.2 | 93 | BULK-506ea227 |
| 13528 | Bulk Product 790d1df9 | Toys & Games | $89.27 | 189 | ★ 4.1 | 13 | BULK-de2b43ce |
| 13529 | Bulk Product 4f8aa339 | Toys & Games | $244.41 | 26 | ★ 0.3 | 27 | BULK-d7abb581 |
| 13530 | Bulk Product ea9f8da3 | Toys & Games | $460.27 | 345 | ★ 1.7 | 62 | BULK-0d038aaa |
| 13531 | Bulk Product a863133f | Home & Garden | $301.55 | 10 | ★ 4.1 | 91 | BULK-7f50421f |
| 13532 | Bulk Product 879d941e | Home & Garden | $757.41 | 199 | ★ 2.5 | 74 | BULK-0830a594 |
| 13533 | Bulk Product 8aadbb44 | Clothing | $269.08 | 398 | ★ 1.5 | 64 | BULK-799973fc |
| 13534 | Bulk Product d5f071cd | Sports & Outdoors | $902.36 | 48 | ★ 0.8 | 27 | BULK-44142c33 |
// 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
};
}