Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 5710 | Bulk Product 503d0735 | Home & Garden | $81.95 | 294 | ★ 3.0 | 63 | BULK-84893852 |
| 5711 | Bulk Product dd53ad3d | Books | $563.67 | 280 | ★ 4.0 | 39 | BULK-825fd17c |
| 5712 | Bulk Product 21f59f79 | Clothing | $658.95 | 492 | ★ 4.1 | 5 | BULK-c8458760 |
| 5713 | Bulk Product bf7af0d7 | Home & Garden | $213.80 | 192 | ★ 2.6 | 56 | BULK-04b6f58f |
| 5714 | Bulk Product 42dbb086 | Clothing | $352.73 | 224 | ★ 4.1 | 72 | BULK-c6a8aaf5 |
| 5715 | Bulk Product cdc64225 | Electronics | $155.58 | 18 | ★ 0.4 | 37 | BULK-6ecc2391 |
| 5716 | Bulk Product 8df3d873 | Books | $970.74 | 331 | ★ 2.0 | 16 | BULK-c1fd6e88 |
| 5717 | Bulk Product 58306a50 | Clothing | $574.30 | 406 | ★ 3.9 | 1 | BULK-2b747c88 |
| 5718 | Bulk Product 0f042d8d | Sports & Outdoors | $855.38 | 412 | ★ 3.4 | 28 | BULK-f89ee503 |
| 5719 | Bulk Product 69aed59a | Sports & Outdoors | $41.12 | 27 | ★ 3.3 | 11 | BULK-9bfb274b |
| 5720 | Bulk Product ab72581f | Sports & Outdoors | $753.98 | 344 | ★ 2.2 | 55 | BULK-974efcc3 |
| 5721 | Bulk Product fb2b306a | Home & Garden | $831.93 | 37 | ★ 2.5 | 82 | BULK-d19bf665 |
| 5722 | Bulk Product 358de939 | Electronics | $272.46 | 21 | ★ 2.1 | 24 | BULK-c8cfedb2 |
| 5723 | Bulk Product a7597c42 | Home & Garden | $316.53 | 259 | ★ 1.9 | 87 | BULK-1e1e3d1c |
| 5724 | Bulk Product 090fab62 | Clothing | $591.28 | 41 | ★ 2.7 | 52 | BULK-07f21c5c |
| 5725 | Bulk Product c3615310 | Toys & Games | $956.03 | 241 | ★ 1.0 | 96 | BULK-5140d4e7 |
| 5726 | Bulk Product 848ad5c4 | Sports & Outdoors | $311.51 | 275 | ★ 0.7 | 19 | BULK-282f45d3 |
| 5727 | Bulk Product 2e8284d4 | Sports & Outdoors | $136.71 | 82 | ★ 2.5 | 71 | BULK-71711e1c |
| 5728 | Bulk Product b9e35f85 | Home & Garden | $393.07 | 128 | ★ 0.3 | 94 | BULK-9e037769 |
| 5729 | Bulk Product d570c018 | Sports & Outdoors | $35.79 | 120 | ★ 2.6 | 54 | BULK-2041ddc5 |
| 5730 | Bulk Product 17026410 | Home & Garden | $253.00 | 163 | ★ 3.4 | 81 | BULK-c5ab6b94 |
| 5731 | Bulk Product 654b8d1f | Electronics | $675.08 | 212 | ★ 3.1 | 70 | BULK-32e186ce |
| 5732 | Bulk Product 1f334037 | Books | $826.50 | 267 | ★ 3.3 | 82 | BULK-45030f90 |
| 5733 | Bulk Product 88face5e | Home & Garden | $714.82 | 194 | ★ 4.2 | 1 | BULK-6b5d6b3b |
| 5734 | Bulk Product 80d8543f | Toys & Games | $976.99 | 11 | ★ 1.6 | 18 | BULK-ae584f34 |
// 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
};
}