Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 22510 | Bulk Product 1585bf32 | Books | $488.96 | 88 | ★ 3.6 | 60 | BULK-27f986fb |
| 22511 | Bulk Product 6212ad4d | Toys & Games | $464.33 | 78 | ★ 2.7 | 21 | BULK-433939eb |
| 22512 | Bulk Product a4be0c60 | Books | $422.93 | 51 | ★ 1.9 | 80 | BULK-6f35a0dd |
| 22513 | Bulk Product 9d882117 | Toys & Games | $713.74 | 254 | ★ 4.2 | 49 | BULK-b6d703d8 |
| 22514 | Bulk Product daa0eb92 | Books | $620.96 | 334 | ★ 2.0 | 12 | BULK-492df9ac |
| 22515 | Bulk Product 0cef680c | Books | $129.66 | 400 | ★ 4.8 | 62 | BULK-aa62f2a6 |
| 22516 | Bulk Product 7c9bb762 | Books | $672.53 | 253 | ★ 2.4 | 52 | BULK-e293c3f9 |
| 22517 | Bulk Product 474a6147 | Clothing | $207.75 | 123 | ★ 1.7 | 53 | BULK-d784a4d3 |
| 22518 | Bulk Product 22013de1 | Home & Garden | $605.40 | 419 | ★ 0.9 | 42 | BULK-cf3e96ad |
| 22519 | Bulk Product 8fda3448 | Sports & Outdoors | $957.37 | 155 | ★ 0.5 | 91 | BULK-c978fa13 |
| 22520 | Bulk Product 32ef11f6 | Toys & Games | $529.85 | 342 | ★ 0.2 | 37 | BULK-ca50d014 |
| 22521 | Bulk Product 5eade75c | Toys & Games | $920.53 | 386 | ★ 0.6 | 37 | BULK-03712cb1 |
| 22522 | Bulk Product f1b5466e | Toys & Games | $847.46 | 68 | ★ 2.8 | 36 | BULK-7607d618 |
| 22523 | Bulk Product fee67d45 | Clothing | $759.58 | 87 | ★ 4.2 | 30 | BULK-b3768001 |
| 22524 | Bulk Product 7d213c63 | Clothing | $208.11 | 59 | ★ 3.4 | 31 | BULK-51327b1f |
| 22525 | Bulk Product 57de703f | Toys & Games | $425.83 | 159 | ★ 4.7 | 36 | BULK-19957af6 |
| 22526 | Bulk Product 77251194 | Toys & Games | $995.06 | 344 | ★ 1.8 | 9 | BULK-8a76dbd3 |
| 22527 | Bulk Product de4e47b9 | Toys & Games | $642.06 | 126 | ★ 3.0 | 51 | BULK-8e1fdbcf |
| 22528 | Bulk Product 19ca8064 | Home & Garden | $120.30 | 195 | ★ 0.4 | 15 | BULK-7e539145 |
| 22529 | Bulk Product 4c1add52 | Home & Garden | $620.79 | 493 | ★ 5.0 | 67 | BULK-e304d423 |
| 22530 | Bulk Product 4f974710 | Electronics | $537.89 | 310 | ★ 1.5 | 39 | BULK-766472f6 |
| 22531 | Bulk Product 709266e9 | Books | $397.29 | 108 | ★ 2.1 | 18 | BULK-89b68946 |
| 22532 | Bulk Product 7f7f8e61 | Electronics | $239.53 | 445 | ★ 0.8 | 77 | BULK-c20625bb |
| 22533 | Bulk Product 1ed61ad3 | Electronics | $81.84 | 452 | ★ 0.2 | 20 | BULK-3c33e6c1 |
| 22534 | Bulk Product b0e82820 | Home & Garden | $372.50 | 491 | ★ 1.7 | 52 | BULK-fe6f5059 |
// 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
};
}