Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 16110 | Bulk Product 2f1493ea | Books | $677.79 | 17 | ★ 0.1 | 22 | BULK-9c574d43 |
| 16111 | Bulk Product 3bd2c68d | Electronics | $209.64 | 217 | ★ 2.4 | 75 | BULK-94e454fb |
| 16112 | Bulk Product ca7486e7 | Sports & Outdoors | $488.42 | 434 | ★ 2.0 | 29 | BULK-946d330a |
| 16113 | Bulk Product d48ae4f2 | Sports & Outdoors | $312.73 | 105 | ★ 3.8 | 92 | BULK-6336befd |
| 16114 | Bulk Product 238e5731 | Home & Garden | $866.14 | 439 | ★ 2.0 | 62 | BULK-6f20481f |
| 16115 | Bulk Product 6f2e84d4 | Books | $273.15 | 400 | ★ 3.7 | 39 | BULK-1b05acb4 |
| 16116 | Bulk Product 69db4df2 | Electronics | $299.15 | 438 | ★ 1.5 | 60 | BULK-f8cd9b26 |
| 16117 | Bulk Product b5632e9c | Sports & Outdoors | $740.13 | 162 | ★ 0.5 | 40 | BULK-8886c795 |
| 16118 | Bulk Product 7053ebcb | Home & Garden | $141.52 | 483 | ★ 4.8 | 64 | BULK-5df2d243 |
| 16119 | Bulk Product e35bf619 | Books | $395.00 | 310 | ★ 2.5 | 9 | BULK-43f976b0 |
| 16120 | Bulk Product b44e80f2 | Toys & Games | $397.01 | 2 | ★ 4.2 | 87 | BULK-00b96ec9 |
| 16121 | Bulk Product 33cead16 | Toys & Games | $717.21 | 155 | ★ 4.7 | 82 | BULK-10c4fb75 |
| 16122 | Bulk Product 2be5b640 | Clothing | $918.96 | 57 | ★ 2.6 | 54 | BULK-a935d2c3 |
| 16123 | Bulk Product 23819177 | Toys & Games | $873.21 | 477 | ★ 5.0 | 61 | BULK-143ec50c |
| 16124 | Bulk Product 54e1687a | Clothing | $444.21 | 394 | ★ 0.1 | 34 | BULK-774b383e |
| 16125 | Bulk Product 9a6e34f0 | Sports & Outdoors | $363.51 | 172 | ★ 1.0 | 73 | BULK-b93b6868 |
| 16126 | Bulk Product 876d4f64 | Electronics | $646.62 | 404 | ★ 4.8 | 21 | BULK-30367e28 |
| 16127 | Bulk Product e1cac02e | Books | $584.13 | 405 | ★ 4.1 | 81 | BULK-9e12babd |
| 16128 | Bulk Product 8d7ed7ee | Home & Garden | $1,009.83 | 386 | ★ 1.6 | 90 | BULK-0b660343 |
| 16129 | Bulk Product a27602d0 | Books | $672.85 | 177 | ★ 2.2 | 7 | BULK-e809464b |
| 16130 | Bulk Product 40f37f5f | Clothing | $885.27 | 194 | ★ 1.7 | 32 | BULK-8b000a7c |
| 16131 | Bulk Product c7612493 | Sports & Outdoors | $340.24 | 295 | ★ 3.7 | 16 | BULK-567b6758 |
| 16132 | Bulk Product 3ad0dd0c | Electronics | $897.92 | 17 | ★ 1.0 | 45 | BULK-9c9a3381 |
| 16133 | Bulk Product 57a1733f | Electronics | $484.44 | 133 | ★ 0.9 | 5 | BULK-b96b21bd |
| 16134 | Bulk Product edc53952 | Electronics | $317.88 | 354 | ★ 4.3 | 64 | BULK-82c10764 |
// 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
};
}