Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 24785 | Bulk Product ef3b9bb7 | Sports & Outdoors | $786.83 | 108 | ★ 2.4 | 47 | BULK-6537b52d |
| 24786 | Bulk Product 1dd8b3e0 | Electronics | $693.46 | 70 | ★ 4.1 | 50 | BULK-a581112c |
| 24787 | Bulk Product 154efc6d | Toys & Games | $948.84 | 86 | ★ 2.3 | 29 | BULK-f2763b73 |
| 24788 | Bulk Product 82bb9cf8 | Home & Garden | $346.50 | 17 | ★ 4.3 | 61 | BULK-8589df53 |
| 24789 | Bulk Product 8a7b441e | Books | $822.97 | 45 | ★ 4.8 | 15 | BULK-8762ea34 |
| 24790 | Bulk Product 3c2effce | Books | $745.45 | 244 | ★ 3.6 | 66 | BULK-a3e2d13b |
| 24791 | Bulk Product 3b829fdb | Clothing | $297.62 | 174 | ★ 1.3 | 41 | BULK-3887e3af |
| 24792 | Bulk Product df4bfe99 | Electronics | $449.53 | 95 | ★ 3.5 | 76 | BULK-27fee8e2 |
| 24793 | Bulk Product 6e9fa0a2 | Electronics | $698.47 | 272 | ★ 3.7 | 72 | BULK-409cae7d |
| 24794 | Bulk Product 23e1801e | Books | $286.20 | 58 | ★ 2.8 | 34 | BULK-fdf8c566 |
| 24795 | Bulk Product 972f9fe3 | Clothing | $735.95 | 30 | ★ 3.1 | 60 | BULK-e90c15db |
| 24796 | Bulk Product 63e1ca51 | Sports & Outdoors | $462.65 | 326 | ★ 3.1 | 46 | BULK-157b1e89 |
| 24797 | Bulk Product 01645a93 | Toys & Games | $251.67 | 409 | ★ 1.3 | 87 | BULK-64a004d4 |
| 24798 | Bulk Product 6d4ab6bd | Clothing | $950.23 | 430 | ★ 4.9 | 36 | BULK-2046f2fc |
| 24799 | Bulk Product fa7f498d | Sports & Outdoors | $796.62 | 438 | ★ 0.0 | 66 | BULK-95ed92f5 |
| 24800 | Bulk Product 651186e5 | Books | $785.09 | 133 | ★ 0.6 | 45 | BULK-0d737403 |
| 24801 | Bulk Product d701fda7 | Home & Garden | $979.19 | 119 | ★ 4.5 | 93 | BULK-b48e899e |
| 24802 | Bulk Product d0f287c3 | Electronics | $356.00 | 34 | ★ 3.9 | 64 | BULK-ce546d13 |
| 24803 | Bulk Product 55c05845 | Sports & Outdoors | $631.37 | 394 | ★ 4.8 | 81 | BULK-3113e6b9 |
| 24804 | Bulk Product 0af0e8cd | Toys & Games | $1,007.91 | 252 | ★ 3.6 | 65 | BULK-77868b97 |
| 24805 | Bulk Product 06944d8b | Home & Garden | $929.37 | 107 | ★ 4.7 | 40 | BULK-ca8e01aa |
| 24806 | Bulk Product 531b6e26 | Clothing | $752.18 | 113 | ★ 1.9 | 72 | BULK-19dd7594 |
| 24807 | Bulk Product 8cc2b44f | Sports & Outdoors | $698.55 | 409 | ★ 1.0 | 51 | BULK-a34d1121 |
| 24808 | Bulk Product 16c2ec5b | Clothing | $537.99 | 63 | ★ 1.7 | 75 | BULK-a860f2bb |
| 24809 | Bulk Product 80259eeb | Clothing | $481.36 | 148 | ★ 0.0 | 15 | BULK-cc1b16dc |
// 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
};
}