Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 21735 | Bulk Product 2fd1b33c | Clothing | $582.13 | 279 | ★ 1.7 | 34 | BULK-52b04c69 |
| 21736 | Bulk Product c8f40fdf | Sports & Outdoors | $607.21 | 137 | ★ 0.7 | 98 | BULK-5cb71944 |
| 21737 | Bulk Product ae5c8f59 | Toys & Games | $935.57 | 188 | ★ 2.1 | 61 | BULK-0687ae1e |
| 21738 | Bulk Product 7c761eb0 | Books | $977.08 | 272 | ★ 1.1 | 86 | BULK-9d2789c0 |
| 21739 | Bulk Product a1d77029 | Toys & Games | $310.15 | 495 | ★ 4.1 | 51 | BULK-7d30034d |
| 21740 | Bulk Product 80f78f64 | Clothing | $338.87 | 18 | ★ 1.4 | 47 | BULK-0538e9c0 |
| 21741 | Bulk Product aef12d54 | Sports & Outdoors | $667.96 | 327 | ★ 0.5 | 87 | BULK-a4902cec |
| 21742 | Bulk Product b7fb44a4 | Clothing | $504.33 | 311 | ★ 1.1 | 76 | BULK-ff81fe05 |
| 21743 | Bulk Product ca57bf66 | Toys & Games | $185.50 | 145 | ★ 0.9 | 52 | BULK-76f03ce7 |
| 21744 | Bulk Product f33a9a68 | Books | $259.98 | 314 | ★ 1.0 | 87 | BULK-c3ccb662 |
| 21745 | Bulk Product 7dc676cf | Clothing | $432.39 | 271 | ★ 4.2 | 27 | BULK-a9134205 |
| 21746 | Bulk Product 3800554f | Home & Garden | $925.26 | 409 | ★ 1.4 | 29 | BULK-c17cba7e |
| 21747 | Bulk Product ea221ab6 | Home & Garden | $771.63 | 425 | ★ 2.3 | 61 | BULK-6f64a120 |
| 21748 | Bulk Product 636cb99d | Clothing | $384.52 | 149 | ★ 2.9 | 98 | BULK-21721822 |
| 21749 | Bulk Product 86e50aa4 | Books | $186.48 | 435 | ★ 1.0 | 76 | BULK-51053675 |
| 21750 | Bulk Product 5a75ff8a | Home & Garden | $530.34 | 451 | ★ 2.9 | 28 | BULK-2842d435 |
| 21751 | Bulk Product df01b07a | Clothing | $483.55 | 250 | ★ 0.5 | 46 | BULK-b67dd025 |
| 21752 | Bulk Product 09de04d0 | Electronics | $349.67 | 201 | ★ 0.9 | 3 | BULK-3a7e0cb6 |
| 21753 | Bulk Product c5751e73 | Sports & Outdoors | $93.89 | 101 | ★ 0.3 | 51 | BULK-1d9d1604 |
| 21754 | Bulk Product 1c9b5cec | Sports & Outdoors | $852.46 | 337 | ★ 2.2 | 97 | BULK-288b069e |
| 21755 | Bulk Product 421c5f07 | Electronics | $902.68 | 225 | ★ 4.6 | 3 | BULK-63b8259f |
| 21756 | Bulk Product 9832ce49 | Electronics | $524.63 | 466 | ★ 4.3 | 46 | BULK-b9dfcee8 |
| 21757 | Bulk Product 51e087a2 | Clothing | $599.42 | 129 | ★ 2.2 | 29 | BULK-e5a49360 |
| 21758 | Bulk Product ca8cd650 | Books | $213.43 | 158 | ★ 4.6 | 26 | BULK-0975524a |
| 21759 | Bulk Product f61aa621 | Electronics | $437.51 | 282 | ★ 4.8 | 24 | BULK-65293858 |
// 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
};
}