Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 10385 | Bulk Product e021772a | Books | $128.62 | 205 | ★ 1.4 | 56 | BULK-2e4e76d1 |
| 10386 | Bulk Product f6fb5300 | Toys & Games | $581.20 | 421 | ★ 4.2 | 3 | BULK-77ee6b89 |
| 10387 | Bulk Product 4edb89ea | Electronics | $542.04 | 345 | ★ 1.9 | 23 | BULK-a39ace45 |
| 10388 | Bulk Product abd21d45 | Clothing | $431.98 | 443 | ★ 1.3 | 55 | BULK-83027a66 |
| 10389 | Bulk Product 58582469 | Home & Garden | $955.26 | 312 | ★ 1.8 | 96 | BULK-5dd6aa47 |
| 10390 | Bulk Product 78a44f9f | Sports & Outdoors | $871.41 | 386 | ★ 0.3 | 83 | BULK-18269b4a |
| 10391 | Bulk Product e2056690 | Clothing | $11.36 | 180 | ★ 4.9 | 80 | BULK-91d9fe1a |
| 10392 | Bulk Product 869d38d8 | Electronics | $996.88 | 303 | ★ 1.9 | 88 | BULK-0a4b23e0 |
| 10393 | Bulk Product 07cfc214 | Home & Garden | $997.88 | 96 | ★ 4.4 | 97 | BULK-fcd7d862 |
| 10394 | Bulk Product d336f50c | Sports & Outdoors | $891.28 | 466 | ★ 0.5 | 94 | BULK-fec8aa04 |
| 10395 | Bulk Product 5ee4fa00 | Clothing | $278.04 | 432 | ★ 0.3 | 11 | BULK-a290040a |
| 10396 | Bulk Product 5a1f3c03 | Books | $274.03 | 117 | ★ 1.9 | 42 | BULK-43e5abba |
| 10397 | Bulk Product 88aef6aa | Books | $911.16 | 106 | ★ 4.5 | 3 | BULK-8ae4b4be |
| 10398 | Bulk Product 14837c11 | Sports & Outdoors | $61.04 | 182 | ★ 1.8 | 26 | BULK-b0ecc98d |
| 10399 | Bulk Product 14940ccb | Electronics | $474.87 | 53 | ★ 3.8 | 30 | BULK-e9eddc03 |
| 10400 | Bulk Product 14bde8bb | Electronics | $289.47 | 293 | ★ 2.4 | 99 | BULK-ba7e5006 |
| 10401 | Bulk Product 9fc36832 | Toys & Games | $538.37 | 96 | ★ 5.0 | 14 | BULK-c4ed7a57 |
| 10402 | Bulk Product bc15c695 | Home & Garden | $60.77 | 63 | ★ 4.5 | 90 | BULK-883dc85f |
| 10403 | Bulk Product 0ab38094 | Home & Garden | $676.21 | 122 | ★ 5.0 | 10 | BULK-8772236e |
| 10404 | Bulk Product 8173ecc2 | Toys & Games | $909.47 | 429 | ★ 0.1 | 1 | BULK-30a0cc6f |
| 10405 | Bulk Product 16a4272e | Sports & Outdoors | $245.88 | 105 | ★ 1.7 | 26 | BULK-51c98bc7 |
| 10406 | Bulk Product bdd40ec9 | Books | $423.05 | 472 | ★ 4.4 | 50 | BULK-1f7fb211 |
| 10407 | Bulk Product 5bd5fa29 | Books | $112.69 | 164 | ★ 0.6 | 61 | BULK-da482568 |
| 10408 | Bulk Product 55b498c1 | Clothing | $411.82 | 449 | ★ 0.9 | 60 | BULK-317a4c07 |
| 10409 | Bulk Product e53765ef | Sports & Outdoors | $282.75 | 19 | ★ 1.3 | 13 | BULK-608af5d6 |
// 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
};
}