Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 14435 | Bulk Product 664c2b19 | Books | $246.92 | 474 | ★ 2.5 | 47 | BULK-4bbcc878 |
| 14436 | Bulk Product 550314e1 | Books | $557.36 | 367 | ★ 4.3 | 30 | BULK-7d897c48 |
| 14437 | Bulk Product 96e5e24a | Electronics | $634.10 | 109 | ★ 4.2 | 48 | BULK-4453eee5 |
| 14438 | Bulk Product 4b415852 | Clothing | $688.74 | 454 | ★ 3.5 | 29 | BULK-0f1ad9bc |
| 14439 | Bulk Product e6ad0dd6 | Books | $668.91 | 480 | ★ 0.8 | 90 | BULK-375fe6ea |
| 14440 | Bulk Product 6c1d62c3 | Home & Garden | $401.89 | 487 | ★ 3.7 | 71 | BULK-db506fa0 |
| 14441 | Bulk Product b65334c4 | Books | $785.72 | 66 | ★ 3.3 | 52 | BULK-00769bc3 |
| 14442 | Bulk Product 45d93a70 | Toys & Games | $779.43 | 294 | ★ 1.3 | 58 | BULK-236f426d |
| 14443 | Bulk Product a00b7685 | Sports & Outdoors | $295.57 | 255 | ★ 2.5 | 72 | BULK-fd2281e9 |
| 14444 | Bulk Product fb742b0d | Clothing | $547.00 | 186 | ★ 0.8 | 22 | BULK-141a62fe |
| 14445 | Bulk Product a56a61f4 | Toys & Games | $594.97 | 305 | ★ 3.0 | 7 | BULK-b33c0f6e |
| 14446 | Bulk Product 89c8f525 | Toys & Games | $402.85 | 445 | ★ 1.0 | 21 | BULK-b1fcf7f5 |
| 14447 | Bulk Product e0d23b64 | Sports & Outdoors | $446.15 | 14 | ★ 2.0 | 11 | BULK-067c5363 |
| 14448 | Bulk Product e1597377 | Sports & Outdoors | $894.30 | 351 | ★ 4.3 | 41 | BULK-56f1448b |
| 14449 | Bulk Product 32bf96d5 | Books | $308.45 | 352 | ★ 1.8 | 47 | BULK-fdd4e11f |
| 14450 | Bulk Product ff093fd9 | Clothing | $53.86 | 329 | ★ 4.6 | 41 | BULK-f4a18475 |
| 14451 | Bulk Product eb094f13 | Sports & Outdoors | $908.55 | 493 | ★ 0.8 | 62 | BULK-c5b559b4 |
| 14452 | Bulk Product a44df668 | Sports & Outdoors | $222.15 | 443 | ★ 0.3 | 8 | BULK-143c7407 |
| 14453 | Bulk Product d60dc59b | Books | $987.64 | 369 | ★ 3.2 | 41 | BULK-58a617cb |
| 14454 | Bulk Product c9f88762 | Home & Garden | $257.93 | 75 | ★ 0.4 | 82 | BULK-937d8b3f |
| 14455 | Bulk Product f1139b3e | Books | $85.86 | 195 | ★ 4.9 | 35 | BULK-8e21257b |
| 14456 | Bulk Product 4d2f8385 | Toys & Games | $507.43 | 259 | ★ 0.1 | 26 | BULK-3b89d8a0 |
| 14457 | Bulk Product 630d82af | Home & Garden | $266.05 | 421 | ★ 4.8 | 91 | BULK-9e3567bb |
| 14458 | Bulk Product aa70712d | Sports & Outdoors | $350.53 | 23 | ★ 0.2 | 16 | BULK-e3feda82 |
| 14459 | Bulk Product fb6998c7 | Sports & Outdoors | $996.98 | 155 | ★ 3.9 | 82 | BULK-245145bf |
// 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
};
}