Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 17685 | Bulk Product 86337407 | Sports & Outdoors | $772.96 | 23 | ★ 4.1 | 10 | BULK-8892df98 |
| 17686 | Bulk Product 1bf4d7c6 | Toys & Games | $648.40 | 82 | ★ 0.9 | 12 | BULK-74c15aa0 |
| 17687 | Bulk Product d7c38a84 | Sports & Outdoors | $644.01 | 351 | ★ 0.7 | 56 | BULK-82b6e810 |
| 17688 | Bulk Product 05d28e8c | Sports & Outdoors | $586.91 | 259 | ★ 0.3 | 89 | BULK-62a56428 |
| 17689 | Bulk Product b3ff06e8 | Clothing | $758.79 | 314 | ★ 1.1 | 40 | BULK-0b66cea1 |
| 17690 | Bulk Product 242e880b | Sports & Outdoors | $477.75 | 265 | ★ 0.7 | 24 | BULK-263d00fb |
| 17691 | Bulk Product 835bef97 | Sports & Outdoors | $42.63 | 286 | ★ 1.3 | 94 | BULK-cbbcf395 |
| 17692 | Bulk Product f250e0f5 | Sports & Outdoors | $572.89 | 279 | ★ 0.8 | 51 | BULK-8d6e33e5 |
| 17693 | Bulk Product a7141f14 | Sports & Outdoors | $921.01 | 177 | ★ 2.6 | 86 | BULK-32bced74 |
| 17694 | Bulk Product fab20c2b | Clothing | $507.17 | 291 | ★ 3.5 | 71 | BULK-0cb16f39 |
| 17695 | Bulk Product 8804b82f | Home & Garden | $525.76 | 103 | ★ 1.3 | 65 | BULK-8e6aefaf |
| 17696 | Bulk Product 0a922eb1 | Clothing | $403.42 | 422 | ★ 0.8 | 7 | BULK-8300f0d3 |
| 17697 | Bulk Product a5236eea | Sports & Outdoors | $258.82 | 68 | ★ 3.6 | 13 | BULK-67da6512 |
| 17698 | Bulk Product 5b8e12b6 | Sports & Outdoors | $865.54 | 135 | ★ 0.5 | 21 | BULK-c4819a23 |
| 17699 | Bulk Product 71d6753c | Toys & Games | $629.32 | 147 | ★ 0.4 | 6 | BULK-17c71a5c |
| 17700 | Bulk Product 039a0e4b | Books | $224.10 | 326 | ★ 0.2 | 9 | BULK-8985e2a5 |
| 17701 | Bulk Product 7c35f0d7 | Sports & Outdoors | $952.06 | 438 | ★ 0.0 | 17 | BULK-9eb70f25 |
| 17702 | Bulk Product a5fde51f | Home & Garden | $326.51 | 11 | ★ 0.3 | 64 | BULK-0679a34a |
| 17703 | Bulk Product 2dd502cd | Sports & Outdoors | $671.71 | 349 | ★ 0.2 | 82 | BULK-287db9bb |
| 17704 | Bulk Product 3a0c7253 | Books | $119.72 | 426 | ★ 2.9 | 24 | BULK-244ad9b7 |
| 17705 | Bulk Product 412ecf94 | Home & Garden | $383.86 | 160 | ★ 2.9 | 63 | BULK-1c8f376f |
| 17706 | Bulk Product a6027d4c | Electronics | $648.96 | 230 | ★ 0.7 | 66 | BULK-4bce2395 |
| 17707 | Bulk Product 4daa260b | Clothing | $770.11 | 2 | ★ 0.6 | 61 | BULK-2c98bec3 |
| 17708 | Bulk Product 6669db60 | Clothing | $687.02 | 372 | ★ 3.0 | 93 | BULK-7854d0d5 |
| 17709 | Bulk Product d1c91c46 | Books | $340.57 | 30 | ★ 3.8 | 92 | BULK-5b45690b |
// 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
};
}