Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 22785 | Bulk Product 2807fc69 | Clothing | $932.42 | 496 | ★ 0.2 | 11 | BULK-512b0f76 |
| 22786 | Bulk Product 686bfd83 | Electronics | $785.97 | 489 | ★ 1.7 | 45 | BULK-86960fd0 |
| 22787 | Bulk Product d6496d14 | Home & Garden | $467.56 | 469 | ★ 2.1 | 36 | BULK-ae30091f |
| 22788 | Bulk Product eb5023aa | Toys & Games | $821.28 | 223 | ★ 0.8 | 38 | BULK-01b8815f |
| 22789 | Bulk Product 98d31e9d | Electronics | $759.96 | 81 | ★ 0.6 | 64 | BULK-fb7b8e5c |
| 22790 | Bulk Product 6e9c5394 | Sports & Outdoors | $151.40 | 284 | ★ 3.3 | 30 | BULK-b2cf7aa9 |
| 22791 | Bulk Product 798e666c | Books | $555.10 | 291 | ★ 1.9 | 28 | BULK-bd8afc2f |
| 22792 | Bulk Product 8ac669e1 | Electronics | $204.40 | 81 | ★ 1.3 | 81 | BULK-fc8b11d4 |
| 22793 | Bulk Product 357454d7 | Home & Garden | $885.52 | 446 | ★ 2.6 | 84 | BULK-e6ff5531 |
| 22794 | Bulk Product 95d3357f | Sports & Outdoors | $292.91 | 154 | ★ 2.2 | 83 | BULK-28ef3811 |
| 22795 | Bulk Product 2ba50fde | Sports & Outdoors | $380.45 | 227 | ★ 3.9 | 61 | BULK-d4962a8b |
| 22796 | Bulk Product ceb61787 | Clothing | $803.39 | 249 | ★ 0.4 | 83 | BULK-e37aad38 |
| 22797 | Bulk Product 4eab2793 | Clothing | $348.66 | 499 | ★ 1.6 | 5 | BULK-aab359a8 |
| 22798 | Bulk Product 6fe9e0e6 | Books | $247.23 | 134 | ★ 2.3 | 76 | BULK-95e91412 |
| 22799 | Bulk Product 6bb18ff6 | Electronics | $225.24 | 72 | ★ 3.9 | 80 | BULK-956a9550 |
| 22800 | Bulk Product b0deec7e | Electronics | $796.30 | 335 | ★ 2.9 | 69 | BULK-b3baa14f |
| 22801 | Bulk Product d04aa0db | Toys & Games | $978.95 | 166 | ★ 3.7 | 86 | BULK-eb313e4b |
| 22802 | Bulk Product 01587062 | Books | $169.23 | 330 | ★ 2.1 | 93 | BULK-b722aa64 |
| 22803 | Bulk Product d3a86e18 | Electronics | $366.29 | 240 | ★ 2.0 | 31 | BULK-ff5736eb |
| 22804 | Bulk Product d8edb2b2 | Sports & Outdoors | $304.82 | 410 | ★ 1.4 | 80 | BULK-ff127a3d |
| 22805 | Bulk Product b86aafcf | Toys & Games | $57.65 | 446 | ★ 3.9 | 57 | BULK-be299165 |
| 22806 | Bulk Product f0d19dc3 | Books | $575.61 | 209 | ★ 4.2 | 50 | BULK-d58ca801 |
| 22807 | Bulk Product 58486435 | Books | $255.78 | 56 | ★ 2.7 | 94 | BULK-7dc4a026 |
| 22808 | Bulk Product 7d2b4379 | Home & Garden | $304.48 | 91 | ★ 0.9 | 69 | BULK-391f83fc |
| 22809 | Bulk Product ce564213 | Toys & Games | $646.39 | 56 | ★ 4.3 | 9 | BULK-0310fbc5 |
// 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
};
}