Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 15585 | Bulk Product a77e3b21 | Sports & Outdoors | $840.93 | 114 | ★ 5.0 | 19 | BULK-c316d670 |
| 15586 | Bulk Product 8ee50321 | Sports & Outdoors | $116.94 | 189 | ★ 4.7 | 89 | BULK-e8b3d4ca |
| 15587 | Bulk Product d8f55046 | Books | $884.77 | 386 | ★ 2.5 | 92 | BULK-4b4d949e |
| 15588 | Bulk Product b37b95dd | Sports & Outdoors | $177.12 | 398 | ★ 1.1 | 8 | BULK-fb5ed161 |
| 15589 | Bulk Product d12e0820 | Electronics | $424.34 | 424 | ★ 4.4 | 39 | BULK-79329297 |
| 15590 | Bulk Product 05245ff9 | Toys & Games | $158.20 | 289 | ★ 4.0 | 15 | BULK-2b9a425a |
| 15591 | Bulk Product 8604338d | Toys & Games | $756.35 | 247 | ★ 2.9 | 74 | BULK-057d926e |
| 15592 | Bulk Product 74ed5621 | Clothing | $553.81 | 416 | ★ 2.5 | 53 | BULK-99dfff23 |
| 15593 | Bulk Product 1e7c0918 | Toys & Games | $248.72 | 247 | ★ 4.3 | 25 | BULK-c896e76e |
| 15594 | Bulk Product 9fdf68ab | Clothing | $239.21 | 39 | ★ 2.9 | 51 | BULK-facf0465 |
| 15595 | Bulk Product ba4b7f03 | Electronics | $403.57 | 497 | ★ 0.7 | 26 | BULK-d778f7c1 |
| 15596 | Bulk Product 2875211c | Toys & Games | $219.04 | 273 | ★ 2.0 | 14 | BULK-650a7c16 |
| 15597 | Bulk Product c22113f8 | Sports & Outdoors | $522.48 | 195 | ★ 0.9 | 49 | BULK-88985369 |
| 15598 | Bulk Product a44d24aa | Clothing | $890.14 | 228 | ★ 0.6 | 50 | BULK-5f17c5c9 |
| 15599 | Bulk Product 0031a61a | Toys & Games | $726.28 | 80 | ★ 2.0 | 18 | BULK-2d573c85 |
| 15600 | Bulk Product 80d71901 | Toys & Games | $588.54 | 335 | ★ 4.2 | 44 | BULK-4d99d820 |
| 15601 | Bulk Product 2ac57f03 | Toys & Games | $634.17 | 73 | ★ 3.3 | 73 | BULK-e36fdfc0 |
| 15602 | Bulk Product 479f1a67 | Electronics | $468.90 | 23 | ★ 4.8 | 39 | BULK-7c2af7e5 |
| 15603 | Bulk Product 90de83f4 | Electronics | $551.95 | 385 | ★ 0.0 | 65 | BULK-fa319922 |
| 15604 | Bulk Product ee8b1b0c | Toys & Games | $978.68 | 386 | ★ 2.2 | 77 | BULK-f270d62e |
| 15605 | Bulk Product b9e8cec3 | Sports & Outdoors | $221.93 | 118 | ★ 1.5 | 35 | BULK-96ae3fd4 |
| 15606 | Bulk Product 742a3ddc | Books | $231.01 | 79 | ★ 4.4 | 64 | BULK-12b95cc5 |
| 15607 | Bulk Product 327d7151 | Toys & Games | $78.09 | 293 | ★ 4.0 | 30 | BULK-29dd2033 |
| 15608 | Bulk Product 0532a9fe | Toys & Games | $599.19 | 203 | ★ 2.4 | 68 | BULK-8e1a42b9 |
| 15609 | Bulk Product a6ff2818 | Books | $616.02 | 32 | ★ 0.0 | 74 | BULK-c34d7082 |
// 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
};
}