Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 20535 | Bulk Product 3fc111d0 | Electronics | $125.78 | 292 | ★ 3.9 | 24 | BULK-8c7a162e |
| 20536 | Bulk Product 363a51a8 | Electronics | $975.48 | 264 | ★ 4.6 | 16 | BULK-d38a489d |
| 20537 | Bulk Product 7f3daec2 | Clothing | $704.35 | 182 | ★ 2.6 | 99 | BULK-b6e80928 |
| 20538 | Bulk Product b612ab30 | Electronics | $199.76 | 223 | ★ 0.3 | 32 | BULK-044347c2 |
| 20539 | Bulk Product 7856e18e | Electronics | $547.38 | 494 | ★ 3.9 | 32 | BULK-e150491d |
| 20540 | Bulk Product 6ade2fb1 | Sports & Outdoors | $135.83 | 278 | ★ 0.3 | 71 | BULK-f85624fc |
| 20541 | Bulk Product 387c39ab | Clothing | $773.98 | 82 | ★ 2.7 | 98 | BULK-93b099de |
| 20542 | Bulk Product b0b4208e | Clothing | $136.39 | 314 | ★ 3.4 | 35 | BULK-afd1b40a |
| 20543 | Bulk Product 2c226dd8 | Books | $87.94 | 484 | ★ 4.2 | 67 | BULK-46976fb8 |
| 20544 | Bulk Product e5cd93b2 | Sports & Outdoors | $557.36 | 255 | ★ 3.2 | 92 | BULK-1f76382a |
| 20545 | Bulk Product 6f251d8f | Clothing | $252.79 | 439 | ★ 3.1 | 40 | BULK-ab231692 |
| 20546 | Bulk Product 12bb48ca | Books | $252.97 | 79 | ★ 1.4 | 62 | BULK-f9715567 |
| 20547 | Bulk Product b9c95ac2 | Electronics | $766.30 | 308 | ★ 0.1 | 88 | BULK-f9a9b3f9 |
| 20548 | Bulk Product 6b2b257d | Sports & Outdoors | $149.32 | 14 | ★ 0.6 | 21 | BULK-15e7c51b |
| 20549 | Bulk Product 16d4e887 | Electronics | $228.71 | 231 | ★ 5.0 | 19 | BULK-aff168cf |
| 20550 | Bulk Product 94fcc5d0 | Books | $865.89 | 327 | ★ 3.0 | 76 | BULK-cd724754 |
| 20551 | Bulk Product 6f679961 | Toys & Games | $440.66 | 400 | ★ 0.2 | 9 | BULK-81494a44 |
| 20552 | Bulk Product 2b87a495 | Toys & Games | $604.55 | 287 | ★ 0.3 | 4 | BULK-2e60d9b6 |
| 20553 | Bulk Product 62b77e88 | Sports & Outdoors | $125.13 | 39 | ★ 2.4 | 35 | BULK-37e9fd6b |
| 20554 | Bulk Product 8ad07101 | Clothing | $895.53 | 83 | ★ 0.2 | 16 | BULK-3a84d2ae |
| 20555 | Bulk Product e500507c | Toys & Games | $28.13 | 145 | ★ 4.1 | 3 | BULK-aca5fb9f |
| 20556 | Bulk Product 139e66c6 | Sports & Outdoors | $274.59 | 92 | ★ 3.4 | 89 | BULK-eca5193b |
| 20557 | Bulk Product 35d6841a | Home & Garden | $69.24 | 62 | ★ 3.6 | 17 | BULK-e370553a |
| 20558 | Bulk Product 3e1f475a | Sports & Outdoors | $949.57 | 185 | ★ 0.6 | 61 | BULK-c2f3862e |
| 20559 | Bulk Product 5d98b29d | Toys & Games | $146.00 | 306 | ★ 0.8 | 2 | BULK-242d6511 |
// 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
};
}