Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 5685 | Bulk Product ba7dbd19 | Sports & Outdoors | $987.07 | 411 | ★ 0.9 | 46 | BULK-2ee441eb |
| 5686 | Bulk Product c7db3cac | Sports & Outdoors | $150.62 | 100 | ★ 1.5 | 1 | BULK-7cab883e |
| 5687 | Bulk Product 26d0ad40 | Toys & Games | $946.35 | 361 | ★ 1.3 | 28 | BULK-70063272 |
| 5688 | Bulk Product d59d9ae5 | Clothing | $104.38 | 16 | ★ 3.5 | 86 | BULK-958fb947 |
| 5689 | Bulk Product 6e1f7638 | Sports & Outdoors | $961.02 | 361 | ★ 4.2 | 56 | BULK-a02987eb |
| 5690 | Bulk Product f74a494e | Sports & Outdoors | $387.49 | 493 | ★ 0.2 | 9 | BULK-20be3f7b |
| 5691 | Bulk Product 7b871cf5 | Electronics | $683.14 | 253 | ★ 3.3 | 95 | BULK-028fc36d |
| 5692 | Bulk Product 6861b689 | Books | $634.57 | 118 | ★ 3.6 | 60 | BULK-420fc6c1 |
| 5693 | Bulk Product 229dba29 | Toys & Games | $616.58 | 285 | ★ 4.5 | 13 | BULK-0f589d9a |
| 5694 | Bulk Product 67672141 | Clothing | $522.82 | 150 | ★ 1.7 | 71 | BULK-ecb0f8bf |
| 5695 | Bulk Product 91436d2c | Books | $798.54 | 100 | ★ 3.1 | 88 | BULK-cac6c9ba |
| 5696 | Bulk Product 5d2a4487 | Sports & Outdoors | $364.20 | 334 | ★ 2.3 | 42 | BULK-e6d4b3cc |
| 5697 | Bulk Product c8b90117 | Toys & Games | $167.97 | 206 | ★ 3.8 | 96 | BULK-057f8128 |
| 5698 | Bulk Product aa7ef09e | Clothing | $102.05 | 194 | ★ 2.7 | 76 | BULK-ef979dd8 |
| 5699 | Bulk Product e127226e | Sports & Outdoors | $873.10 | 102 | ★ 0.7 | 5 | BULK-f2f28512 |
| 5700 | Bulk Product 58fde25e | Electronics | $1,005.12 | 207 | ★ 2.2 | 47 | BULK-5cd73399 |
| 5701 | Bulk Product 9d74f3f6 | Clothing | $428.89 | 163 | ★ 4.6 | 68 | BULK-03868f45 |
| 5702 | Bulk Product 87241ec4 | Electronics | $294.78 | 336 | ★ 3.0 | 43 | BULK-eabb482c |
| 5703 | Bulk Product 9414c9e3 | Books | $540.65 | 463 | ★ 0.6 | 80 | BULK-15a7e05c |
| 5704 | Bulk Product 4cfe5a40 | Sports & Outdoors | $28.41 | 403 | ★ 2.2 | 98 | BULK-561d4423 |
| 5705 | Bulk Product 82f7ca81 | Clothing | $728.45 | 155 | ★ 0.8 | 66 | BULK-ede9c3f2 |
| 5706 | Bulk Product 1fdba3f9 | Books | $264.87 | 465 | ★ 0.0 | 78 | BULK-e8b6ed4a |
| 5707 | Bulk Product 7fc22ca7 | Books | $970.08 | 283 | ★ 3.5 | 63 | BULK-8459ab91 |
| 5708 | Bulk Product f59fff16 | Books | $82.92 | 443 | ★ 3.2 | 23 | BULK-4c0acfa0 |
| 5709 | Bulk Product b2ecd304 | Books | $396.38 | 390 | ★ 4.9 | 96 | BULK-25256a05 |
// 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
};
}