Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 12960 | Bulk Product 01dd4ea1 | Home & Garden | $198.09 | 376 | ★ 0.3 | 30 | BULK-1d6882b0 |
| 12961 | Bulk Product 880861e9 | Toys & Games | $275.21 | 278 | ★ 4.1 | 44 | BULK-22145fcf |
| 12962 | Bulk Product a404ccf5 | Toys & Games | $305.10 | 288 | ★ 1.1 | 85 | BULK-8ffe668a |
| 12963 | Bulk Product cc820121 | Books | $562.98 | 332 | ★ 4.9 | 40 | BULK-1929a506 |
| 12964 | Bulk Product d4a65a57 | Electronics | $377.48 | 345 | ★ 2.7 | 50 | BULK-002e6244 |
| 12965 | Bulk Product c7e8fff7 | Toys & Games | $58.27 | 481 | ★ 2.9 | 34 | BULK-c1c4fa55 |
| 12966 | Bulk Product b5fc291e | Toys & Games | $961.78 | 171 | ★ 4.6 | 95 | BULK-fcf2975e |
| 12967 | Bulk Product 1eb48441 | Electronics | $387.92 | 159 | ★ 4.9 | 17 | BULK-26c06ef7 |
| 12968 | Bulk Product 051cb6dc | Electronics | $66.48 | 470 | ★ 5.0 | 31 | BULK-89d0d4d8 |
| 12969 | Bulk Product 1cfac1b6 | Sports & Outdoors | $608.27 | 410 | ★ 3.0 | 36 | BULK-22be4d3e |
| 12970 | Bulk Product e24aad42 | Books | $962.95 | 451 | ★ 2.6 | 3 | BULK-b3dd349a |
| 12971 | Bulk Product fe8edca9 | Clothing | $897.04 | 449 | ★ 3.0 | 62 | BULK-cc3bd39a |
| 12972 | Bulk Product 62c4795b | Electronics | $27.81 | 26 | ★ 2.8 | 8 | BULK-cb16b364 |
| 12973 | Bulk Product 0c047d92 | Books | $709.42 | 344 | ★ 0.4 | 51 | BULK-02ac168a |
| 12974 | Bulk Product decf1c2f | Electronics | $914.38 | 415 | ★ 2.5 | 79 | BULK-b08154ba |
| 12975 | Bulk Product c726391d | Clothing | $893.08 | 438 | ★ 3.6 | 67 | BULK-6e27e840 |
| 12976 | Bulk Product fce40c70 | Clothing | $883.81 | 99 | ★ 3.6 | 79 | BULK-e50cb3de |
| 12977 | Bulk Product ae37d3b9 | Books | $1,008.69 | 174 | ★ 4.8 | 1 | BULK-58b7d138 |
| 12978 | Bulk Product 9b59284d | Toys & Games | $994.02 | 114 | ★ 1.1 | 69 | BULK-9abe2b65 |
| 12979 | Bulk Product 1f38d685 | Toys & Games | $276.35 | 310 | ★ 4.4 | 98 | BULK-e5faf153 |
| 12980 | Bulk Product 8f17b252 | Clothing | $16.36 | 391 | ★ 1.6 | 35 | BULK-bf99a25f |
| 12981 | Bulk Product 88179051 | Toys & Games | $170.57 | 13 | ★ 2.8 | 5 | BULK-de8761ba |
| 12982 | Bulk Product 0e9b8edd | Toys & Games | $402.17 | 267 | ★ 1.8 | 33 | BULK-7ccf028f |
| 12983 | Bulk Product 64850168 | Toys & Games | $761.18 | 276 | ★ 1.6 | 69 | BULK-bf9f4a67 |
| 12984 | Bulk Product 67d4295c | Clothing | $618.73 | 310 | ★ 1.8 | 54 | BULK-37cdcb7d |
// 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
};
}