Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 21660 | Bulk Product e7a3c381 | Sports & Outdoors | $998.01 | 362 | ★ 3.3 | 94 | BULK-f2d731fa |
| 21661 | Bulk Product a83fa841 | Toys & Games | $403.65 | 438 | ★ 4.5 | 94 | BULK-6d468a5d |
| 21662 | Bulk Product 79178f5d | Electronics | $586.17 | 394 | ★ 2.1 | 40 | BULK-05f1801b |
| 21663 | Bulk Product 9c0f4a4e | Sports & Outdoors | $463.94 | 289 | ★ 0.0 | 38 | BULK-f61c34ab |
| 21664 | Bulk Product 0a237ac4 | Sports & Outdoors | $947.81 | 225 | ★ 1.3 | 66 | BULK-33a50dc7 |
| 21665 | Bulk Product 4a709f84 | Clothing | $729.35 | 172 | ★ 3.6 | 82 | BULK-f3a44300 |
| 21666 | Bulk Product 668d4ef9 | Home & Garden | $793.57 | 325 | ★ 4.4 | 26 | BULK-8e8c1e25 |
| 21667 | Bulk Product c83b74b6 | Toys & Games | $107.00 | 196 | ★ 3.7 | 82 | BULK-cefc4775 |
| 21668 | Bulk Product 2b5f8846 | Clothing | $867.44 | 59 | ★ 3.4 | 42 | BULK-77b4701d |
| 21669 | Bulk Product 83306450 | Electronics | $546.02 | 35 | ★ 3.6 | 63 | BULK-eb59536a |
| 21670 | Bulk Product abf6b863 | Clothing | $692.85 | 135 | ★ 3.4 | 46 | BULK-f762898e |
| 21671 | Bulk Product bc62d1c9 | Electronics | $456.88 | 450 | ★ 4.6 | 18 | BULK-6527ee9b |
| 21672 | Bulk Product b45ecc66 | Toys & Games | $691.89 | 277 | ★ 3.4 | 70 | BULK-61737369 |
| 21673 | Bulk Product 3ec837cf | Sports & Outdoors | $67.43 | 118 | ★ 3.1 | 13 | BULK-cdd97fa9 |
| 21674 | Bulk Product ebb620ed | Toys & Games | $38.38 | 142 | ★ 1.5 | 41 | BULK-2dfaeda7 |
| 21675 | Bulk Product aa0b8948 | Sports & Outdoors | $554.95 | 56 | ★ 4.0 | 15 | BULK-719968e4 |
| 21676 | Bulk Product 42547f4b | Home & Garden | $914.51 | 381 | ★ 2.6 | 26 | BULK-07f63ac3 |
| 21677 | Bulk Product 73ad6028 | Toys & Games | $378.60 | 114 | ★ 0.4 | 69 | BULK-b8041610 |
| 21678 | Bulk Product 110af790 | Electronics | $369.40 | 104 | ★ 4.9 | 1 | BULK-5f723219 |
| 21679 | Bulk Product eacf81e1 | Home & Garden | $248.76 | 67 | ★ 1.0 | 40 | BULK-949e4f4d |
| 21680 | Bulk Product 2d6292dc | Clothing | $539.36 | 68 | ★ 3.7 | 77 | BULK-ef4a2d9c |
| 21681 | Bulk Product 45f13670 | Books | $486.90 | 322 | ★ 1.7 | 31 | BULK-c461c3a7 |
| 21682 | Bulk Product fe14b986 | Electronics | $875.75 | 36 | ★ 4.2 | 35 | BULK-596379bc |
| 21683 | Bulk Product bf4f81a8 | Clothing | $143.98 | 183 | ★ 4.8 | 14 | BULK-7c1befe4 |
| 21684 | Bulk Product 24021dd6 | Toys & Games | $928.33 | 94 | ★ 0.3 | 17 | BULK-bb299867 |
// 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
};
}