Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 15560 | Bulk Product 4c0af675 | Electronics | $849.46 | 493 | ★ 2.2 | 51 | BULK-a4057eb0 |
| 15561 | Bulk Product ee894a45 | Toys & Games | $814.36 | 284 | ★ 1.3 | 43 | BULK-0f9535d8 |
| 15562 | Bulk Product 82557e1f | Sports & Outdoors | $409.29 | 184 | ★ 2.0 | 39 | BULK-beefff4d |
| 15563 | Bulk Product 81d8353d | Toys & Games | $882.46 | 484 | ★ 1.4 | 66 | BULK-4c1822cc |
| 15564 | Bulk Product 4db9ceff | Home & Garden | $542.01 | 331 | ★ 4.1 | 44 | BULK-014e5b48 |
| 15565 | Bulk Product 2620e24c | Clothing | $147.34 | 174 | ★ 2.0 | 15 | BULK-89c0efaa |
| 15566 | Bulk Product e994d3d0 | Books | $276.12 | 417 | ★ 3.5 | 73 | BULK-6987a75c |
| 15567 | Bulk Product 89eebb8b | Electronics | $96.40 | 233 | ★ 2.5 | 92 | BULK-1ebfec48 |
| 15568 | Bulk Product f70e9a12 | Clothing | $140.86 | 230 | ★ 1.3 | 67 | BULK-8e25a87f |
| 15569 | Bulk Product 2a800260 | Clothing | $519.83 | 282 | ★ 0.4 | 99 | BULK-fdc2c568 |
| 15570 | Bulk Product c38f1ae1 | Sports & Outdoors | $384.61 | 168 | ★ 0.1 | 1 | BULK-68df6c4b |
| 15571 | Bulk Product 495e1b4d | Toys & Games | $279.37 | 267 | ★ 2.7 | 20 | BULK-4238774a |
| 15572 | Bulk Product 4bea1552 | Clothing | $187.96 | 195 | ★ 2.4 | 27 | BULK-1e1bd93b |
| 15573 | Bulk Product 0c478298 | Books | $396.20 | 215 | ★ 1.5 | 65 | BULK-43ce64c5 |
| 15574 | Bulk Product d8765f7d | Clothing | $646.67 | 36 | ★ 0.2 | 65 | BULK-e51dde51 |
| 15575 | Bulk Product 55dc128c | Books | $928.49 | 408 | ★ 3.0 | 6 | BULK-4c2ee116 |
| 15576 | Bulk Product 7d2f232f | Electronics | $423.67 | 127 | ★ 1.4 | 59 | BULK-8b765262 |
| 15577 | Bulk Product a9bfe5dd | Electronics | $296.76 | 118 | ★ 1.3 | 11 | BULK-f9bf53be |
| 15578 | Bulk Product bdd536b5 | Home & Garden | $400.46 | 51 | ★ 2.9 | 97 | BULK-7a82c90c |
| 15579 | Bulk Product b11d1e57 | Books | $153.31 | 140 | ★ 2.4 | 43 | BULK-c25a84cb |
| 15580 | Bulk Product 70437161 | Home & Garden | $854.76 | 322 | ★ 2.5 | 98 | BULK-961543b8 |
| 15581 | Bulk Product 63f21d0a | Toys & Games | $697.63 | 140 | ★ 1.4 | 80 | BULK-a13bba13 |
| 15582 | Bulk Product e64ff499 | Sports & Outdoors | $165.25 | 57 | ★ 1.6 | 27 | BULK-aa2eed9c |
| 15583 | Bulk Product 36eff9d8 | Books | $26.91 | 386 | ★ 4.2 | 49 | BULK-08846dd7 |
| 15584 | Bulk Product 78cd725a | Clothing | $184.58 | 389 | ★ 0.2 | 94 | BULK-ed832693 |
// 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
};
}