Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 17310 | Bulk Product 390566a2 | Electronics | $115.65 | 447 | ★ 2.5 | 86 | BULK-2c0fea5b |
| 17311 | Bulk Product 5f2f345d | Home & Garden | $391.78 | 95 | ★ 4.1 | 34 | BULK-2542ebe9 |
| 17312 | Bulk Product 86691b60 | Clothing | $96.10 | 250 | ★ 1.3 | 87 | BULK-66349058 |
| 17313 | Bulk Product c1aa3312 | Electronics | $279.37 | 499 | ★ 0.3 | 55 | BULK-b162f1c0 |
| 17314 | Bulk Product 55421f8c | Books | $463.34 | 293 | ★ 1.1 | 60 | BULK-f2876826 |
| 17315 | Bulk Product 56d3fa66 | Toys & Games | $161.78 | 162 | ★ 2.0 | 89 | BULK-2f3b9aa2 |
| 17316 | Bulk Product 91d6d124 | Home & Garden | $822.29 | 46 | ★ 4.8 | 75 | BULK-cfd72429 |
| 17317 | Bulk Product 1c1d1f69 | Clothing | $842.39 | 45 | ★ 0.3 | 88 | BULK-2d0781fe |
| 17318 | Bulk Product ad67910e | Toys & Games | $133.04 | 313 | ★ 3.0 | 96 | BULK-6973e49f |
| 17319 | Bulk Product bcfb4e6a | Sports & Outdoors | $24.32 | 55 | ★ 0.6 | 59 | BULK-d87ea64e |
| 17320 | Bulk Product e757e7d2 | Home & Garden | $742.64 | 168 | ★ 4.1 | 13 | BULK-6cfb2a9b |
| 17321 | Bulk Product 5cc14850 | Home & Garden | $869.95 | 210 | ★ 2.9 | 13 | BULK-c31b32d2 |
| 17322 | Bulk Product 1aecc86c | Books | $591.79 | 289 | ★ 5.0 | 80 | BULK-8df2e20f |
| 17323 | Bulk Product b443b1dc | Sports & Outdoors | $626.50 | 202 | ★ 0.7 | 44 | BULK-f7dd457a |
| 17324 | Bulk Product 972a6bbf | Electronics | $64.59 | 338 | ★ 4.3 | 59 | BULK-9c12cee2 |
| 17325 | Bulk Product 354243ae | Home & Garden | $935.49 | 112 | ★ 0.3 | 8 | BULK-5eb75623 |
| 17326 | Bulk Product 9fc51b00 | Books | $938.44 | 450 | ★ 2.5 | 36 | BULK-d3e6fcf9 |
| 17327 | Bulk Product bcebe373 | Clothing | $203.19 | 224 | ★ 1.1 | 71 | BULK-935f0912 |
| 17328 | Bulk Product 17c45bcd | Sports & Outdoors | $44.94 | 449 | ★ 1.5 | 66 | BULK-90d43796 |
| 17329 | Bulk Product 270f05af | Books | $658.33 | 313 | ★ 0.4 | 54 | BULK-ac6f727d |
| 17330 | Bulk Product dba7c627 | Sports & Outdoors | $666.83 | 22 | ★ 0.6 | 6 | BULK-83e26e3d |
| 17331 | Bulk Product fe3bd83c | Books | $447.00 | 423 | ★ 3.5 | 19 | BULK-46bc945e |
| 17332 | Bulk Product c6d8c1f0 | Toys & Games | $60.94 | 337 | ★ 2.6 | 48 | BULK-7a79ad54 |
| 17333 | Bulk Product de2e5b41 | Home & Garden | $533.03 | 281 | ★ 0.6 | 63 | BULK-35677032 |
| 17334 | Bulk Product a309c243 | Sports & Outdoors | $490.66 | 78 | ★ 1.4 | 35 | BULK-943ee3cd |
// 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
};
}