Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 23510 | Bulk Product d58b8702 | Sports & Outdoors | $408.98 | 409 | ★ 2.7 | 55 | BULK-e4eb910d |
| 23511 | Bulk Product ce966fbe | Sports & Outdoors | $426.99 | 481 | ★ 0.7 | 12 | BULK-a3db6812 |
| 23512 | Bulk Product 933f1ee3 | Electronics | $670.78 | 423 | ★ 1.3 | 3 | BULK-2c28fb5b |
| 23513 | Bulk Product f7604dd6 | Toys & Games | $672.64 | 386 | ★ 4.9 | 38 | BULK-b57e1495 |
| 23514 | Bulk Product b7343708 | Home & Garden | $761.15 | 120 | ★ 2.0 | 17 | BULK-44a3dd67 |
| 23515 | Bulk Product 758810e2 | Home & Garden | $134.95 | 465 | ★ 1.9 | 11 | BULK-23e9d0d7 |
| 23516 | Bulk Product 234fa277 | Sports & Outdoors | $277.61 | 271 | ★ 3.2 | 91 | BULK-231265fc |
| 23517 | Bulk Product 4a18a2bd | Books | $282.04 | 449 | ★ 3.0 | 9 | BULK-04f1b038 |
| 23518 | Bulk Product 54382fc7 | Clothing | $311.29 | 87 | ★ 1.7 | 15 | BULK-d100ab02 |
| 23519 | Bulk Product 09563180 | Clothing | $412.87 | 366 | ★ 0.7 | 18 | BULK-303598e8 |
| 23520 | Bulk Product 7ec756c6 | Toys & Games | $933.28 | 193 | ★ 5.0 | 62 | BULK-7a988293 |
| 23521 | Bulk Product 5f34ee3a | Sports & Outdoors | $939.50 | 270 | ★ 1.7 | 21 | BULK-64ab4442 |
| 23522 | Bulk Product b950d510 | Electronics | $218.67 | 160 | ★ 4.1 | 91 | BULK-34caf852 |
| 23523 | Bulk Product 6c0fbcbb | Books | $202.66 | 365 | ★ 4.1 | 34 | BULK-5fa5411c |
| 23524 | Bulk Product 58a09d46 | Clothing | $483.76 | 143 | ★ 4.7 | 15 | BULK-037dc983 |
| 23525 | Bulk Product 0ecdd67d | Home & Garden | $803.99 | 466 | ★ 4.7 | 49 | BULK-914ee53f |
| 23526 | Bulk Product 88a4c86d | Sports & Outdoors | $129.83 | 49 | ★ 2.5 | 84 | BULK-15da5b8e |
| 23527 | Bulk Product 72e18d17 | Clothing | $822.80 | 462 | ★ 1.8 | 72 | BULK-45e82c7f |
| 23528 | Bulk Product 16cd87e2 | Home & Garden | $277.90 | 167 | ★ 0.9 | 77 | BULK-5176d8a7 |
| 23529 | Bulk Product 357db3c4 | Electronics | $827.10 | 14 | ★ 4.4 | 51 | BULK-3d70a971 |
| 23530 | Bulk Product 4dc7dbab | Home & Garden | $470.37 | 355 | ★ 2.3 | 51 | BULK-68f02dc6 |
| 23531 | Bulk Product fd33d38d | Electronics | $930.10 | 200 | ★ 1.5 | 60 | BULK-d752102b |
| 23532 | Bulk Product 4cfcb9f7 | Clothing | $705.98 | 360 | ★ 3.0 | 20 | BULK-6369223a |
| 23533 | Bulk Product e3918c48 | Sports & Outdoors | $395.72 | 230 | ★ 4.8 | 44 | BULK-c4facc61 |
| 23534 | Bulk Product 0556b97a | Sports & Outdoors | $37.58 | 406 | ★ 3.8 | 91 | BULK-b2b629ba |
// 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
};
}