Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 12685 | Bulk Product c4aa8ef9 | Toys & Games | $942.54 | 417 | ★ 0.7 | 5 | BULK-82218064 |
| 12686 | Bulk Product eb402f3c | Books | $657.06 | 495 | ★ 2.9 | 52 | BULK-d714675f |
| 12687 | Bulk Product b2ace0d1 | Clothing | $398.16 | 254 | ★ 2.0 | 58 | BULK-f994e0c5 |
| 12688 | Bulk Product d10e3d43 | Clothing | $303.71 | 185 | ★ 2.3 | 50 | BULK-3fd76008 |
| 12689 | Bulk Product 100a06db | Clothing | $715.69 | 79 | ★ 1.8 | 43 | BULK-e86d4cf6 |
| 12690 | Bulk Product c83beddc | Books | $372.57 | 347 | ★ 1.6 | 0 | BULK-3c73edcb |
| 12691 | Bulk Product 307f5c13 | Toys & Games | $761.65 | 388 | ★ 4.6 | 66 | BULK-9e4663a7 |
| 12692 | Bulk Product 5869c4a6 | Electronics | $528.09 | 315 | ★ 2.1 | 56 | BULK-cfca55d9 |
| 12693 | Bulk Product eea432a3 | Electronics | $232.82 | 355 | ★ 0.9 | 82 | BULK-8d2fc5c0 |
| 12694 | Bulk Product 7e17241c | Clothing | $173.06 | 465 | ★ 2.2 | 74 | BULK-f2d98098 |
| 12695 | Bulk Product 3ae9cdfd | Clothing | $562.74 | 195 | ★ 3.1 | 24 | BULK-eb0f06f9 |
| 12696 | Bulk Product ead94c90 | Toys & Games | $928.01 | 276 | ★ 1.8 | 13 | BULK-5c7758b9 |
| 12697 | Bulk Product 6bc8f1b3 | Books | $362.04 | 290 | ★ 4.5 | 66 | BULK-47751b8a |
| 12698 | Bulk Product bdc80887 | Sports & Outdoors | $913.21 | 250 | ★ 4.6 | 10 | BULK-9755401a |
| 12699 | Bulk Product 98e01feb | Books | $422.85 | 293 | ★ 1.3 | 48 | BULK-2540bfee |
| 12700 | Bulk Product e82a5ae5 | Clothing | $330.68 | 432 | ★ 1.4 | 5 | BULK-d1ce0dc5 |
| 12701 | Bulk Product e9f07e1c | Toys & Games | $860.54 | 151 | ★ 2.9 | 61 | BULK-16104628 |
| 12702 | Bulk Product ea56cb3a | Home & Garden | $500.00 | 52 | ★ 4.9 | 34 | BULK-3e0bbb1f |
| 12703 | Bulk Product fdf7f698 | Books | $212.81 | 54 | ★ 3.4 | 3 | BULK-29da68d6 |
| 12704 | Bulk Product fcc5220e | Toys & Games | $852.84 | 269 | ★ 4.9 | 53 | BULK-fd68e348 |
| 12705 | Bulk Product 50df22ed | Sports & Outdoors | $235.87 | 61 | ★ 4.0 | 41 | BULK-8dd0673d |
| 12706 | Bulk Product ac3b9d88 | Sports & Outdoors | $316.24 | 457 | ★ 2.8 | 90 | BULK-faeb0149 |
| 12707 | Bulk Product fbec67b1 | Books | $895.55 | 254 | ★ 1.0 | 52 | BULK-141b3636 |
| 12708 | Bulk Product eabac6cd | Home & Garden | $527.75 | 65 | ★ 0.9 | 1 | BULK-70f41987 |
| 12709 | Bulk Product e8a4f809 | Sports & Outdoors | $244.11 | 443 | ★ 2.9 | 24 | BULK-6cd51e0a |
// 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
};
}