Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 18035 | Bulk Product 74cff3df | Books | $346.63 | 278 | ★ 3.8 | 95 | BULK-77ee89a9 |
| 18036 | Bulk Product b3149060 | Electronics | $140.83 | 13 | ★ 0.6 | 79 | BULK-eb4d834e |
| 18037 | Bulk Product a4e1fb4c | Home & Garden | $868.45 | 79 | ★ 0.9 | 32 | BULK-18eb0a26 |
| 18038 | Bulk Product a2cce560 | Clothing | $762.01 | 57 | ★ 2.4 | 26 | BULK-7b7a5319 |
| 18039 | Bulk Product 2c7e59ce | Books | $646.85 | 474 | ★ 2.9 | 11 | BULK-dd59e0af |
| 18040 | Bulk Product 88214149 | Home & Garden | $59.32 | 47 | ★ 2.1 | 56 | BULK-c767a231 |
| 18041 | Bulk Product 4276a30b | Sports & Outdoors | $739.26 | 5 | ★ 3.7 | 81 | BULK-7503479e |
| 18042 | Bulk Product 2896e56f | Sports & Outdoors | $923.19 | 355 | ★ 3.6 | 5 | BULK-f231d212 |
| 18043 | Bulk Product 50e029c6 | Books | $379.69 | 40 | ★ 3.6 | 69 | BULK-8274cd71 |
| 18044 | Bulk Product 56388660 | Electronics | $669.40 | 289 | ★ 4.5 | 13 | BULK-3365a84d |
| 18045 | Bulk Product e12eff3f | Clothing | $688.92 | 407 | ★ 1.1 | 92 | BULK-82603c96 |
| 18046 | Bulk Product af88334d | Electronics | $922.17 | 271 | ★ 1.6 | 22 | BULK-ccfc15f6 |
| 18047 | Bulk Product 8ac4489b | Books | $312.89 | 40 | ★ 0.4 | 23 | BULK-512a0305 |
| 18048 | Bulk Product 4f272c64 | Electronics | $486.73 | 39 | ★ 2.3 | 37 | BULK-bc67c20f |
| 18049 | Bulk Product 95632c08 | Books | $369.96 | 114 | ★ 0.2 | 97 | BULK-b2839057 |
| 18050 | Bulk Product cd806e6d | Toys & Games | $587.71 | 389 | ★ 1.5 | 68 | BULK-e952cc86 |
| 18051 | Bulk Product eee37a82 | Sports & Outdoors | $938.43 | 252 | ★ 4.5 | 59 | BULK-36a50d46 |
| 18052 | Bulk Product c9b10299 | Toys & Games | $674.15 | 237 | ★ 0.0 | 91 | BULK-5f3b1e48 |
| 18053 | Bulk Product 379d2df3 | Sports & Outdoors | $500.59 | 247 | ★ 2.9 | 40 | BULK-77ed05fb |
| 18054 | Bulk Product 4da4afdf | Sports & Outdoors | $371.09 | 454 | ★ 1.3 | 98 | BULK-95a5ecbd |
| 18055 | Bulk Product a3e0ffd2 | Clothing | $984.88 | 294 | ★ 4.1 | 0 | BULK-1b01f232 |
| 18056 | Bulk Product 872b9819 | Books | $559.05 | 412 | ★ 2.6 | 84 | BULK-d57657ca |
| 18057 | Bulk Product 9429e61b | Clothing | $981.16 | 340 | ★ 3.1 | 54 | BULK-4e13bc86 |
| 18058 | Bulk Product eb5b8c7d | Sports & Outdoors | $416.32 | 281 | ★ 4.9 | 50 | BULK-b5429734 |
| 18059 | Bulk Product 34752ab8 | Electronics | $527.30 | 369 | ★ 3.3 | 89 | BULK-0e0fdb18 |
// 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
};
}