Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 7510 | Bulk Product 2e643233 | Books | $13.57 | 30 | ★ 3.3 | 56 | BULK-c14984a4 |
| 7511 | Bulk Product 43b6905d | Sports & Outdoors | $49.51 | 161 | ★ 1.1 | 61 | BULK-b2ef30d2 |
| 7512 | Bulk Product e1c6f323 | Sports & Outdoors | $115.13 | 390 | ★ 4.2 | 58 | BULK-6e44fead |
| 7513 | Bulk Product 528a3e07 | Sports & Outdoors | $466.49 | 438 | ★ 1.0 | 33 | BULK-67dd5522 |
| 7514 | Bulk Product 7ca397f9 | Electronics | $896.41 | 103 | ★ 2.2 | 47 | BULK-4da98e79 |
| 7515 | Bulk Product b23e1c27 | Books | $791.55 | 397 | ★ 3.3 | 65 | BULK-5990d2e9 |
| 7516 | Bulk Product 51302c31 | Electronics | $343.93 | 131 | ★ 2.9 | 31 | BULK-592e1592 |
| 7517 | Bulk Product 9f48c033 | Toys & Games | $682.51 | 8 | ★ 2.7 | 89 | BULK-ec867941 |
| 7518 | Bulk Product b43b3ca7 | Clothing | $698.97 | 290 | ★ 0.8 | 0 | BULK-f22f6bfd |
| 7519 | Bulk Product bf95ebbe | Home & Garden | $485.83 | 161 | ★ 3.0 | 83 | BULK-c1bd5572 |
| 7520 | Bulk Product d222a4e2 | Home & Garden | $504.01 | 291 | ★ 2.3 | 52 | BULK-247c074f |
| 7521 | Bulk Product f1013ba3 | Home & Garden | $990.96 | 326 | ★ 5.0 | 27 | BULK-82773032 |
| 7522 | Bulk Product cd53b963 | Clothing | $635.39 | 323 | ★ 1.5 | 66 | BULK-c2d94a40 |
| 7523 | Bulk Product 41559b7c | Sports & Outdoors | $730.62 | 148 | ★ 0.0 | 61 | BULK-8e413d46 |
| 7524 | Bulk Product 2c20ad66 | Electronics | $467.35 | 244 | ★ 0.7 | 8 | BULK-fc0e86e2 |
| 7525 | Bulk Product 992659cd | Toys & Games | $504.76 | 62 | ★ 0.6 | 62 | BULK-19bebc99 |
| 7526 | Bulk Product 385475d6 | Clothing | $928.72 | 217 | ★ 0.7 | 55 | BULK-3513e83d |
| 7527 | Bulk Product 97c83ffc | Electronics | $521.34 | 339 | ★ 2.2 | 52 | BULK-f11b725a |
| 7528 | Bulk Product e6b8f5a8 | Sports & Outdoors | $369.44 | 482 | ★ 3.0 | 74 | BULK-8065d889 |
| 7529 | Bulk Product 99ca82c6 | Electronics | $688.79 | 151 | ★ 4.7 | 64 | BULK-ea49633f |
| 7530 | Bulk Product 1b19ae31 | Toys & Games | $243.96 | 22 | ★ 4.7 | 2 | BULK-290e297a |
| 7531 | Bulk Product 663fbd5b | Electronics | $369.10 | 198 | ★ 1.9 | 61 | BULK-51b15b83 |
| 7532 | Bulk Product 75a18d99 | Toys & Games | $954.25 | 224 | ★ 1.5 | 78 | BULK-8a6c1dd7 |
| 7533 | Bulk Product 8680395a | Books | $192.61 | 415 | ★ 4.8 | 14 | BULK-9cfe448f |
| 7534 | Bulk Product 2e6cbd29 | Books | $234.06 | 214 | ★ 0.2 | 81 | BULK-aec5833d |
// 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
};
}