Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 22735 | Bulk Product d5df4fa5 | Home & Garden | $633.38 | 180 | ★ 2.0 | 42 | BULK-701d2ea6 |
| 22736 | Bulk Product df0a35bf | Electronics | $130.28 | 46 | ★ 5.0 | 89 | BULK-4d991e46 |
| 22737 | Bulk Product 3d97c8d0 | Clothing | $145.03 | 214 | ★ 1.4 | 76 | BULK-a2a1b355 |
| 22738 | Bulk Product c255bd81 | Sports & Outdoors | $377.48 | 222 | ★ 0.4 | 26 | BULK-34d41482 |
| 22739 | Bulk Product 0bc53ccc | Clothing | $923.70 | 316 | ★ 1.1 | 12 | BULK-e70aa214 |
| 22740 | Bulk Product ba37bb7f | Books | $216.34 | 295 | ★ 2.6 | 66 | BULK-d07ba75a |
| 22741 | Bulk Product 891c4ad3 | Sports & Outdoors | $284.35 | 114 | ★ 1.7 | 51 | BULK-d97ecdc0 |
| 22742 | Bulk Product c597d03f | Home & Garden | $980.18 | 209 | ★ 1.2 | 53 | BULK-1dda6f9e |
| 22743 | Bulk Product e6d38b13 | Electronics | $388.13 | 294 | ★ 3.6 | 9 | BULK-38ec9d6b |
| 22744 | Bulk Product 37a222b9 | Electronics | $513.47 | 271 | ★ 2.0 | 47 | BULK-457223df |
| 22745 | Bulk Product 271b7799 | Clothing | $587.48 | 86 | ★ 2.6 | 4 | BULK-a434039b |
| 22746 | Bulk Product 150c1324 | Electronics | $150.62 | 150 | ★ 2.9 | 64 | BULK-05306eba |
| 22747 | Bulk Product beccec06 | Clothing | $783.06 | 324 | ★ 0.1 | 53 | BULK-4fedb129 |
| 22748 | Bulk Product fcb59f73 | Toys & Games | $705.23 | 492 | ★ 4.3 | 84 | BULK-ecfddbe7 |
| 22749 | Bulk Product 51113581 | Sports & Outdoors | $891.89 | 200 | ★ 1.0 | 74 | BULK-99802656 |
| 22750 | Bulk Product ac51e9f9 | Clothing | $929.64 | 204 | ★ 4.3 | 17 | BULK-8708a592 |
| 22751 | Bulk Product 8a1093da | Books | $616.34 | 328 | ★ 0.2 | 70 | BULK-2e33bc91 |
| 22752 | Bulk Product 622716ce | Electronics | $481.41 | 496 | ★ 4.5 | 44 | BULK-00287c72 |
| 22753 | Bulk Product da0e3b3d | Electronics | $869.40 | 216 | ★ 0.4 | 61 | BULK-6662bf5f |
| 22754 | Bulk Product 00b0adaa | Home & Garden | $978.01 | 201 | ★ 3.6 | 43 | BULK-e9a71a1c |
| 22755 | Bulk Product 2bac44ce | Electronics | $1,005.52 | 47 | ★ 0.7 | 4 | BULK-1b449e1c |
| 22756 | Bulk Product 291987ac | Electronics | $396.64 | 453 | ★ 1.2 | 48 | BULK-b8e46387 |
| 22757 | Bulk Product 8290899c | Sports & Outdoors | $988.76 | 217 | ★ 4.9 | 66 | BULK-0dceef17 |
| 22758 | Bulk Product 47a3f51e | Books | $33.48 | 81 | ★ 3.6 | 38 | BULK-1dfad983 |
| 22759 | Bulk Product b3ddcbee | Home & Garden | $776.80 | 139 | ★ 0.7 | 29 | BULK-c339ba68 |
// 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
};
}