Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 14610 | Bulk Product cf73c054 | Home & Garden | $722.75 | 366 | ★ 4.1 | 61 | BULK-42215697 |
| 14611 | Bulk Product 76176de5 | Electronics | $167.38 | 409 | ★ 3.3 | 87 | BULK-56477596 |
| 14612 | Bulk Product 672f831d | Sports & Outdoors | $188.40 | 358 | ★ 0.1 | 26 | BULK-2e47000d |
| 14613 | Bulk Product 10ea4e5b | Electronics | $86.85 | 340 | ★ 4.1 | 4 | BULK-80c0a13c |
| 14614 | Bulk Product a560252e | Toys & Games | $451.66 | 195 | ★ 0.5 | 69 | BULK-660323d3 |
| 14615 | Bulk Product 8a438d20 | Clothing | $831.89 | 167 | ★ 1.3 | 67 | BULK-1da09881 |
| 14616 | Bulk Product a8f7cf55 | Toys & Games | $330.09 | 360 | ★ 2.8 | 33 | BULK-495a63a5 |
| 14617 | Bulk Product 20a2e495 | Electronics | $574.96 | 81 | ★ 2.5 | 7 | BULK-57025070 |
| 14618 | Bulk Product 619d3fa2 | Toys & Games | $463.23 | 373 | ★ 4.7 | 19 | BULK-1921bfe5 |
| 14619 | Bulk Product dc3f09bc | Electronics | $582.38 | 489 | ★ 0.4 | 48 | BULK-7da0f166 |
| 14620 | Bulk Product 8fbd9210 | Home & Garden | $972.01 | 137 | ★ 0.4 | 87 | BULK-e19812f8 |
| 14621 | Bulk Product 9842e472 | Electronics | $185.74 | 342 | ★ 1.1 | 47 | BULK-3e43725c |
| 14622 | Bulk Product 2d7abd58 | Sports & Outdoors | $405.22 | 164 | ★ 1.9 | 59 | BULK-5051cd0b |
| 14623 | Bulk Product 9f576a3c | Clothing | $554.71 | 225 | ★ 4.8 | 4 | BULK-7a7f9ca2 |
| 14624 | Bulk Product 43ed346c | Electronics | $877.95 | 322 | ★ 1.1 | 49 | BULK-db3108f4 |
| 14625 | Bulk Product a2b59b53 | Books | $667.99 | 479 | ★ 2.9 | 75 | BULK-e2fcdf32 |
| 14626 | Bulk Product 5fe7102c | Sports & Outdoors | $304.46 | 217 | ★ 0.1 | 42 | BULK-8149e0ae |
| 14627 | Bulk Product 9c605000 | Home & Garden | $851.19 | 411 | ★ 4.6 | 18 | BULK-3490e54f |
| 14628 | Bulk Product c2f48650 | Toys & Games | $777.37 | 176 | ★ 2.2 | 4 | BULK-87015e5c |
| 14629 | Bulk Product 7278fab6 | Clothing | $934.35 | 81 | ★ 2.7 | 52 | BULK-ca347b11 |
| 14630 | Bulk Product 2bcb6fc6 | Sports & Outdoors | $225.96 | 476 | ★ 1.4 | 82 | BULK-a20b334f |
| 14631 | Bulk Product a12f6f53 | Electronics | $519.15 | 164 | ★ 4.2 | 60 | BULK-4c66d4e3 |
| 14632 | Bulk Product ffc01366 | Books | $913.19 | 41 | ★ 2.2 | 4 | BULK-bc14b1b9 |
| 14633 | Bulk Product 3d8121d7 | Home & Garden | $48.17 | 97 | ★ 4.1 | 9 | BULK-938abd3b |
| 14634 | Bulk Product 376976ec | Toys & Games | $435.75 | 75 | ★ 1.0 | 35 | BULK-e444bb94 |
// 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
};
}