Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 18560 | Bulk Product 3e98297e | Clothing | $781.80 | 284 | ★ 0.2 | 16 | BULK-8fa158cb |
| 18561 | Bulk Product 6bb128ea | Toys & Games | $591.47 | 428 | ★ 4.9 | 38 | BULK-f52cbe50 |
| 18562 | Bulk Product bc9a26d6 | Clothing | $628.08 | 303 | ★ 4.3 | 9 | BULK-bc93a451 |
| 18563 | Bulk Product bc651dba | Sports & Outdoors | $954.74 | 54 | ★ 1.9 | 73 | BULK-39f078ee |
| 18564 | Bulk Product 25482623 | Books | $755.17 | 274 | ★ 3.9 | 11 | BULK-92f45482 |
| 18565 | Bulk Product 74f1f7e3 | Toys & Games | $708.52 | 462 | ★ 3.8 | 34 | BULK-bcb502fd |
| 18566 | Bulk Product a1fdbbf6 | Electronics | $258.76 | 60 | ★ 0.8 | 63 | BULK-7eccb82c |
| 18567 | Bulk Product 739d60f4 | Electronics | $706.21 | 270 | ★ 0.7 | 48 | BULK-98186ace |
| 18568 | Bulk Product 0219bc78 | Clothing | $825.20 | 249 | ★ 3.8 | 11 | BULK-ff37b861 |
| 18569 | Bulk Product 6b33af50 | Clothing | $648.59 | 214 | ★ 2.0 | 83 | BULK-0f04ea86 |
| 18570 | Bulk Product 57ea12b8 | Home & Garden | $798.13 | 134 | ★ 4.8 | 51 | BULK-abc90162 |
| 18571 | Bulk Product a632c82c | Books | $386.34 | 370 | ★ 3.0 | 21 | BULK-0a0b71fc |
| 18572 | Bulk Product 06dcfcdd | Books | $270.25 | 380 | ★ 1.5 | 28 | BULK-6e5484b9 |
| 18573 | Bulk Product e9f24998 | Clothing | $142.57 | 249 | ★ 4.2 | 11 | BULK-cf2551b3 |
| 18574 | Bulk Product 5bd6caf2 | Home & Garden | $649.57 | 67 | ★ 0.2 | 35 | BULK-c80e2eb8 |
| 18575 | Bulk Product 54f801ec | Clothing | $659.45 | 344 | ★ 4.0 | 33 | BULK-31e66cba |
| 18576 | Bulk Product 131080af | Toys & Games | $418.59 | 144 | ★ 4.0 | 61 | BULK-4c10571c |
| 18577 | Bulk Product 957301e9 | Home & Garden | $841.22 | 24 | ★ 2.7 | 0 | BULK-6975eca2 |
| 18578 | Bulk Product f27d3236 | Sports & Outdoors | $486.74 | 386 | ★ 0.3 | 7 | BULK-14c9c678 |
| 18579 | Bulk Product 42b9829c | Home & Garden | $739.45 | 71 | ★ 1.5 | 58 | BULK-461b9b94 |
| 18580 | Bulk Product 6676a1a2 | Clothing | $241.72 | 196 | ★ 1.5 | 98 | BULK-7f30a4da |
| 18581 | Bulk Product 9fcd7159 | Toys & Games | $128.92 | 192 | ★ 4.9 | 32 | BULK-be57e39c |
| 18582 | Bulk Product c711de3f | Electronics | $579.78 | 313 | ★ 0.9 | 3 | BULK-e5f8f2b4 |
| 18583 | Bulk Product db537671 | Electronics | $433.62 | 276 | ★ 0.1 | 95 | BULK-2ccc5a1c |
| 18584 | Bulk Product 1c2d2acb | Clothing | $426.72 | 17 | ★ 1.8 | 45 | BULK-24be0cd9 |
// 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
};
}