Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 5635 | Bulk Product 9ae3f01d | Books | $87.17 | 338 | ★ 3.1 | 48 | BULK-cd822b23 |
| 5636 | Bulk Product 010462b2 | Books | $329.72 | 436 | ★ 0.6 | 93 | BULK-1d99962b |
| 5637 | Bulk Product 22934e68 | Home & Garden | $24.45 | 114 | ★ 2.2 | 88 | BULK-d3ae0568 |
| 5638 | Bulk Product 41db7671 | Sports & Outdoors | $107.46 | 401 | ★ 3.4 | 38 | BULK-e5cf6119 |
| 5639 | Bulk Product f689c1c4 | Electronics | $140.69 | 440 | ★ 1.1 | 61 | BULK-b55946d3 |
| 5640 | Bulk Product 9560f3f3 | Home & Garden | $657.03 | 206 | ★ 2.7 | 47 | BULK-f502a321 |
| 5641 | Bulk Product 99d9a0e8 | Toys & Games | $382.54 | 61 | ★ 4.5 | 71 | BULK-d0d55fef |
| 5642 | Bulk Product d3bd465d | Home & Garden | $123.17 | 470 | ★ 3.4 | 66 | BULK-7c06323c |
| 5643 | Bulk Product f686c680 | Clothing | $422.42 | 194 | ★ 1.6 | 97 | BULK-c086c752 |
| 5644 | Bulk Product e14cc7d3 | Electronics | $514.15 | 60 | ★ 0.7 | 10 | BULK-cd895964 |
| 5645 | Bulk Product b68f9b48 | Home & Garden | $967.14 | 224 | ★ 2.3 | 54 | BULK-a1c6bd76 |
| 5646 | Bulk Product 68ef674d | Toys & Games | $847.57 | 41 | ★ 4.6 | 93 | BULK-8ea4e115 |
| 5647 | Bulk Product d05610fb | Clothing | $349.24 | 338 | ★ 1.8 | 89 | BULK-6243b788 |
| 5648 | Bulk Product 54023a48 | Electronics | $549.97 | 169 | ★ 1.5 | 69 | BULK-89017097 |
| 5649 | Bulk Product afc305fb | Clothing | $31.73 | 423 | ★ 2.1 | 73 | BULK-ca8f38ee |
| 5650 | Bulk Product b2d7dd6a | Toys & Games | $161.33 | 396 | ★ 4.0 | 95 | BULK-e0ee2716 |
| 5651 | Bulk Product c0329719 | Home & Garden | $506.61 | 275 | ★ 4.7 | 86 | BULK-42f86607 |
| 5652 | Bulk Product ab986854 | Sports & Outdoors | $560.68 | 315 | ★ 2.8 | 4 | BULK-0267dfc4 |
| 5653 | Bulk Product 70f59035 | Home & Garden | $495.82 | 268 | ★ 4.2 | 83 | BULK-beda4d5c |
| 5654 | Bulk Product 0311795a | Toys & Games | $554.58 | 482 | ★ 3.9 | 26 | BULK-4b7e0ecc |
| 5655 | Bulk Product 2fc91141 | Books | $996.79 | 137 | ★ 1.8 | 69 | BULK-382bbddc |
| 5656 | Bulk Product cc82f0a4 | Clothing | $911.96 | 128 | ★ 0.3 | 43 | BULK-d2582f8f |
| 5657 | Bulk Product c8570d1c | Toys & Games | $320.50 | 33 | ★ 1.8 | 8 | BULK-15768a1c |
| 5658 | Bulk Product 4774c4c9 | Home & Garden | $401.92 | 115 | ★ 1.9 | 96 | BULK-9e5ecb1c |
| 5659 | Bulk Product fc52da6e | Sports & Outdoors | $790.11 | 234 | ★ 3.0 | 27 | BULK-ed85b940 |
// 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
};
}