Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 8635 | Bulk Product 8497115e | Clothing | $85.87 | 186 | ★ 4.8 | 63 | BULK-a42e20c7 |
| 8636 | Bulk Product 0ad26ea7 | Home & Garden | $135.07 | 187 | ★ 3.1 | 69 | BULK-5e73299e |
| 8637 | Bulk Product d0d85af6 | Sports & Outdoors | $471.28 | 210 | ★ 0.7 | 5 | BULK-db42737c |
| 8638 | Bulk Product 9fcebec6 | Electronics | $786.95 | 393 | ★ 0.7 | 67 | BULK-aba0177c |
| 8639 | Bulk Product c75caa1f | Toys & Games | $676.21 | 73 | ★ 4.0 | 72 | BULK-c5fe4609 |
| 8640 | Bulk Product f8332f74 | Electronics | $85.74 | 163 | ★ 2.1 | 74 | BULK-00c34ecb |
| 8641 | Bulk Product 766380ad | Books | $391.77 | 323 | ★ 1.7 | 50 | BULK-6e788671 |
| 8642 | Bulk Product 91832947 | Toys & Games | $126.14 | 115 | ★ 1.7 | 68 | BULK-24736b31 |
| 8643 | Bulk Product 8b165c56 | Electronics | $720.54 | 310 | ★ 1.7 | 12 | BULK-bff3434a |
| 8644 | Bulk Product 55ad1a01 | Books | $639.40 | 450 | ★ 2.1 | 97 | BULK-22feedff |
| 8645 | Bulk Product 4ffcdee0 | Sports & Outdoors | $133.07 | 157 | ★ 3.0 | 8 | BULK-3d5c4ab8 |
| 8646 | Bulk Product bc406968 | Clothing | $861.81 | 484 | ★ 4.5 | 64 | BULK-e149f6e8 |
| 8647 | Bulk Product d353c534 | Books | $889.45 | 8 | ★ 0.2 | 83 | BULK-d0d3c664 |
| 8648 | Bulk Product 4f56d505 | Sports & Outdoors | $949.28 | 131 | ★ 2.9 | 12 | BULK-9fb5bdfb |
| 8649 | Bulk Product 0f452f53 | Electronics | $723.39 | 109 | ★ 4.7 | 23 | BULK-4d89d1b4 |
| 8650 | Bulk Product 527acc4d | Clothing | $632.22 | 454 | ★ 1.0 | 74 | BULK-bd8e95ed |
| 8651 | Bulk Product 7014847f | Books | $796.97 | 382 | ★ 2.3 | 61 | BULK-9dbdf6e6 |
| 8652 | Bulk Product 0b9123a1 | Home & Garden | $464.52 | 12 | ★ 2.4 | 47 | BULK-20384f81 |
| 8653 | Bulk Product a8407c39 | Toys & Games | $780.71 | 329 | ★ 2.3 | 12 | BULK-09ea1a7e |
| 8654 | Bulk Product 35da8f86 | Home & Garden | $735.41 | 151 | ★ 3.0 | 46 | BULK-71121234 |
| 8655 | Bulk Product 8a49db69 | Electronics | $385.83 | 395 | ★ 1.5 | 91 | BULK-809bcc4a |
| 8656 | Bulk Product 66c09764 | Electronics | $174.67 | 14 | ★ 5.0 | 72 | BULK-ebdd29a2 |
| 8657 | Bulk Product e3e6a004 | Electronics | $722.39 | 406 | ★ 0.9 | 77 | BULK-6cd35f3a |
| 8658 | Bulk Product abd22068 | Books | $28.50 | 53 | ★ 0.8 | 52 | BULK-00cab3b8 |
| 8659 | Bulk Product 2ceaf370 | Books | $743.99 | 358 | ★ 2.8 | 63 | BULK-9189cca4 |
// 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
};
}