Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 12410 | Bulk Product 8e84a1f5 | Electronics | $233.57 | 151 | ★ 2.9 | 67 | BULK-4a976b59 |
| 12411 | Bulk Product d725fa82 | Toys & Games | $880.50 | 419 | ★ 0.1 | 52 | BULK-bd8581cb |
| 12412 | Bulk Product 6037d24f | Home & Garden | $100.58 | 312 | ★ 1.6 | 65 | BULK-c137284b |
| 12413 | Bulk Product 814b7b6a | Electronics | $86.93 | 43 | ★ 2.0 | 51 | BULK-d96d40ce |
| 12414 | Bulk Product 72a88fbf | Electronics | $145.12 | 87 | ★ 1.7 | 76 | BULK-889885d1 |
| 12415 | Bulk Product 98121532 | Toys & Games | $752.17 | 150 | ★ 4.7 | 81 | BULK-636f8c86 |
| 12416 | Bulk Product 7c8b4aa4 | Books | $243.17 | 121 | ★ 1.1 | 27 | BULK-a1748faa |
| 12417 | Bulk Product 0b0c40bd | Books | $138.72 | 395 | ★ 0.6 | 4 | BULK-6a00fa35 |
| 12418 | Bulk Product 8b820b04 | Clothing | $81.50 | 448 | ★ 3.5 | 25 | BULK-38925b7f |
| 12419 | Bulk Product a1fb8b2d | Toys & Games | $659.96 | 442 | ★ 1.8 | 57 | BULK-5ce9bf00 |
| 12420 | Bulk Product 80c6dc4e | Clothing | $32.34 | 442 | ★ 2.4 | 13 | BULK-d9a6bc4e |
| 12421 | Bulk Product c2835731 | Sports & Outdoors | $563.20 | 410 | ★ 1.4 | 10 | BULK-9ccf5253 |
| 12422 | Bulk Product b2b0fa9d | Toys & Games | $488.56 | 79 | ★ 0.7 | 0 | BULK-dfbf465b |
| 12423 | Bulk Product f1a17dc6 | Electronics | $703.11 | 17 | ★ 0.9 | 83 | BULK-3d2e3598 |
| 12424 | Bulk Product 9d8c232e | Home & Garden | $994.22 | 473 | ★ 4.9 | 14 | BULK-da06a1a1 |
| 12425 | Bulk Product d0f597da | Clothing | $350.94 | 31 | ★ 4.6 | 13 | BULK-a48e2edd |
| 12426 | Bulk Product 0351234e | Clothing | $222.31 | 403 | ★ 1.3 | 63 | BULK-0ad2d741 |
| 12427 | Bulk Product 858a88aa | Electronics | $679.75 | 4 | ★ 1.5 | 77 | BULK-473b3cc3 |
| 12428 | Bulk Product bce0801b | Books | $283.21 | 478 | ★ 3.9 | 25 | BULK-c8f41f13 |
| 12429 | Bulk Product 544d5ade | Toys & Games | $247.96 | 0 | ★ 3.7 | 99 | BULK-90abeb77 |
| 12430 | Bulk Product ca777a51 | Sports & Outdoors | $244.10 | 71 | ★ 0.1 | 3 | BULK-4b1ea188 |
| 12431 | Bulk Product 15ca056d | Sports & Outdoors | $954.40 | 367 | ★ 3.7 | 49 | BULK-e209fa35 |
| 12432 | Bulk Product 5e6a30d1 | Books | $773.54 | 240 | ★ 1.2 | 75 | BULK-692c0e13 |
| 12433 | Bulk Product 70de3df9 | Sports & Outdoors | $754.18 | 169 | ★ 0.1 | 18 | BULK-624e7adb |
| 12434 | Bulk Product 2b677fe7 | Books | $1,007.39 | 373 | ★ 0.6 | 50 | BULK-b0b5dec8 |
// 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
};
}