Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 18360 | Bulk Product f0493ab3 | Sports & Outdoors | $43.17 | 83 | ★ 3.1 | 59 | BULK-f8687006 |
| 18361 | Bulk Product f790f6ea | Sports & Outdoors | $945.51 | 192 | ★ 4.0 | 35 | BULK-da73773f |
| 18362 | Bulk Product cc66b2e7 | Toys & Games | $518.08 | 304 | ★ 3.0 | 48 | BULK-f3775660 |
| 18363 | Bulk Product c81bbb15 | Clothing | $507.74 | 223 | ★ 3.8 | 47 | BULK-eb7b829d |
| 18364 | Bulk Product 23526a21 | Electronics | $709.83 | 226 | ★ 4.0 | 18 | BULK-6d7b4b6e |
| 18365 | Bulk Product 3283d391 | Home & Garden | $127.30 | 387 | ★ 3.5 | 88 | BULK-63af3b63 |
| 18366 | Bulk Product 3368822a | Sports & Outdoors | $691.91 | 441 | ★ 4.9 | 1 | BULK-55ca72c3 |
| 18367 | Bulk Product edd20d6a | Clothing | $642.67 | 272 | ★ 0.6 | 17 | BULK-97be9425 |
| 18368 | Bulk Product 156b19c2 | Electronics | $512.63 | 204 | ★ 0.3 | 23 | BULK-98366aa5 |
| 18369 | Bulk Product 708a5f49 | Electronics | $748.25 | 115 | ★ 2.2 | 13 | BULK-158ae823 |
| 18370 | Bulk Product 39d7fccb | Books | $813.69 | 454 | ★ 3.8 | 59 | BULK-19d3ea2f |
| 18371 | Bulk Product 88abfc7a | Home & Garden | $437.05 | 159 | ★ 4.6 | 32 | BULK-fffe5f93 |
| 18372 | Bulk Product be8ffd1e | Sports & Outdoors | $438.80 | 192 | ★ 3.4 | 11 | BULK-00a1495a |
| 18373 | Bulk Product cabfc6ac | Books | $858.16 | 498 | ★ 1.9 | 68 | BULK-148dab0f |
| 18374 | Bulk Product 8217070d | Books | $700.19 | 153 | ★ 4.5 | 90 | BULK-16b51ce3 |
| 18375 | Bulk Product ebed931d | Books | $238.86 | 373 | ★ 0.7 | 68 | BULK-8fad8f05 |
| 18376 | Bulk Product 06f8b19e | Home & Garden | $251.28 | 233 | ★ 2.0 | 26 | BULK-5e2db349 |
| 18377 | Bulk Product a1c27b97 | Books | $242.36 | 167 | ★ 0.4 | 70 | BULK-cfb301f5 |
| 18378 | Bulk Product c9e043d4 | Books | $209.96 | 304 | ★ 0.2 | 33 | BULK-1aaaf37c |
| 18379 | Bulk Product 3d246bd5 | Electronics | $883.78 | 248 | ★ 0.3 | 14 | BULK-2b0fc6d0 |
| 18380 | Bulk Product fb89db66 | Home & Garden | $115.45 | 192 | ★ 2.6 | 91 | BULK-fda800a5 |
| 18381 | Bulk Product dc260aa0 | Home & Garden | $399.03 | 178 | ★ 2.0 | 40 | BULK-3c4edcfb |
| 18382 | Bulk Product 9e893dc4 | Toys & Games | $69.37 | 488 | ★ 3.0 | 66 | BULK-df40785e |
| 18383 | Bulk Product 99bcd346 | Sports & Outdoors | $299.99 | 197 | ★ 1.4 | 75 | BULK-297946ae |
| 18384 | Bulk Product 3cfe6a1e | Books | $994.74 | 359 | ★ 0.7 | 35 | BULK-e2f9a39f |
// 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
};
}