Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 12810 | Bulk Product 85e3d4d0 | Books | $380.36 | 232 | ★ 5.0 | 53 | BULK-5163c23d |
| 12811 | Bulk Product 8ceca978 | Home & Garden | $824.06 | 395 | ★ 3.4 | 52 | BULK-29fcabd0 |
| 12812 | Bulk Product b739a312 | Home & Garden | $875.24 | 165 | ★ 1.5 | 83 | BULK-08110828 |
| 12813 | Bulk Product 9706e5c1 | Sports & Outdoors | $114.54 | 462 | ★ 2.0 | 99 | BULK-b4e8471d |
| 12814 | Bulk Product 45711e32 | Books | $172.72 | 308 | ★ 3.6 | 94 | BULK-b5fce0cf |
| 12815 | Bulk Product 5d0a8e59 | Electronics | $975.50 | 344 | ★ 2.1 | 42 | BULK-7ea596c7 |
| 12816 | Bulk Product 78a02704 | Clothing | $703.29 | 424 | ★ 4.9 | 81 | BULK-2fa0acde |
| 12817 | Bulk Product b732ee2a | Clothing | $52.69 | 374 | ★ 3.9 | 92 | BULK-ab77f158 |
| 12818 | Bulk Product e17bb916 | Toys & Games | $675.25 | 37 | ★ 3.8 | 66 | BULK-c2c3c5c7 |
| 12819 | Bulk Product dcdddad9 | Toys & Games | $379.03 | 371 | ★ 0.2 | 46 | BULK-e906fac5 |
| 12820 | Bulk Product 1356cdcd | Clothing | $290.33 | 496 | ★ 3.3 | 29 | BULK-69cfb7f8 |
| 12821 | Bulk Product 7c4fbbb5 | Toys & Games | $310.12 | 38 | ★ 4.8 | 94 | BULK-286f0c25 |
| 12822 | Bulk Product 16150868 | Clothing | $337.38 | 188 | ★ 2.2 | 27 | BULK-ca50c28b |
| 12823 | Bulk Product e6ba1a15 | Home & Garden | $169.92 | 19 | ★ 1.8 | 94 | BULK-290086e7 |
| 12824 | Bulk Product 387d485d | Books | $578.32 | 450 | ★ 4.6 | 68 | BULK-eba1999f |
| 12825 | Bulk Product a1f71ad8 | Home & Garden | $560.34 | 216 | ★ 0.4 | 89 | BULK-99e58c51 |
| 12826 | Bulk Product cc227fe3 | Toys & Games | $919.15 | 179 | ★ 4.2 | 53 | BULK-13f2ff4b |
| 12827 | Bulk Product e333bc04 | Home & Garden | $848.92 | 385 | ★ 1.9 | 58 | BULK-2fa6c123 |
| 12828 | Bulk Product 1846f706 | Sports & Outdoors | $870.34 | 256 | ★ 2.7 | 67 | BULK-765625e8 |
| 12829 | Bulk Product cbc96281 | Toys & Games | $164.00 | 423 | ★ 2.1 | 20 | BULK-d48cefde |
| 12830 | Bulk Product 8b657859 | Toys & Games | $164.69 | 162 | ★ 2.4 | 16 | BULK-9fee9cde |
| 12831 | Bulk Product 6f74234f | Home & Garden | $931.61 | 402 | ★ 4.7 | 6 | BULK-f1e585a1 |
| 12832 | Bulk Product f0657c75 | Books | $671.39 | 494 | ★ 3.4 | 30 | BULK-3686bfa9 |
| 12833 | Bulk Product e0cdd333 | Home & Garden | $162.87 | 327 | ★ 4.3 | 71 | BULK-24e68448 |
| 12834 | Bulk Product 153904e7 | Books | $282.63 | 444 | ★ 3.4 | 10 | BULK-54aef5a6 |
// 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
};
}