Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 19760 | Bulk Product 054005f4 | Sports & Outdoors | $947.17 | 231 | ★ 3.0 | 81 | BULK-e881c7ee |
| 19761 | Bulk Product f10e5cb2 | Electronics | $984.46 | 226 | ★ 3.4 | 56 | BULK-81e06de1 |
| 19762 | Bulk Product d3989c07 | Sports & Outdoors | $357.83 | 255 | ★ 4.8 | 17 | BULK-2fc78484 |
| 19763 | Bulk Product 32d03f22 | Electronics | $206.48 | 364 | ★ 4.7 | 26 | BULK-9bab7382 |
| 19764 | Bulk Product 35211777 | Sports & Outdoors | $556.38 | 322 | ★ 3.9 | 74 | BULK-d00debe4 |
| 19765 | Bulk Product aadc53d1 | Sports & Outdoors | $54.00 | 432 | ★ 2.7 | 10 | BULK-0794075b |
| 19766 | Bulk Product c9101d3e | Clothing | $362.01 | 320 | ★ 1.5 | 19 | BULK-e98b60b2 |
| 19767 | Bulk Product 399ca27c | Toys & Games | $185.67 | 142 | ★ 2.8 | 95 | BULK-ac241c9c |
| 19768 | Bulk Product cd134d29 | Sports & Outdoors | $624.74 | 258 | ★ 4.1 | 74 | BULK-499898fc |
| 19769 | Bulk Product ecf9602a | Clothing | $508.84 | 156 | ★ 0.6 | 18 | BULK-b11765f6 |
| 19770 | Bulk Product af470f0c | Electronics | $759.30 | 187 | ★ 1.2 | 4 | BULK-108cbf77 |
| 19771 | Bulk Product 37fd5215 | Sports & Outdoors | $87.42 | 258 | ★ 3.1 | 91 | BULK-76d839c8 |
| 19772 | Bulk Product 04163b98 | Clothing | $337.61 | 110 | ★ 4.1 | 94 | BULK-4e172b83 |
| 19773 | Bulk Product 6583f1a2 | Clothing | $416.73 | 321 | ★ 0.9 | 9 | BULK-b1309aa0 |
| 19774 | Bulk Product 256bf2f4 | Clothing | $541.68 | 331 | ★ 2.2 | 64 | BULK-c5ebb1a8 |
| 19775 | Bulk Product 6285d0bf | Toys & Games | $253.56 | 35 | ★ 2.6 | 38 | BULK-249d6f5d |
| 19776 | Bulk Product 9cbf938a | Electronics | $644.46 | 188 | ★ 4.4 | 91 | BULK-1e6d4101 |
| 19777 | Bulk Product 85b8bb71 | Home & Garden | $801.26 | 443 | ★ 3.6 | 89 | BULK-4f1d1dcf |
| 19778 | Bulk Product 4ed34f89 | Books | $731.64 | 41 | ★ 5.0 | 50 | BULK-8cef2b9b |
| 19779 | Bulk Product f22de828 | Clothing | $487.90 | 248 | ★ 4.7 | 82 | BULK-ce315bae |
| 19780 | Bulk Product 89159b06 | Clothing | $39.61 | 497 | ★ 4.6 | 74 | BULK-85b78725 |
| 19781 | Bulk Product 8e26cc97 | Home & Garden | $829.24 | 382 | ★ 0.1 | 62 | BULK-6a41361c |
| 19782 | Bulk Product 95882bff | Electronics | $837.92 | 449 | ★ 1.1 | 52 | BULK-3863d049 |
| 19783 | Bulk Product 3077695c | Home & Garden | $618.49 | 95 | ★ 2.1 | 21 | BULK-4323e765 |
| 19784 | Bulk Product b07eaac3 | Books | $802.93 | 306 | ★ 4.9 | 45 | BULK-4036613d |
// 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
};
}