Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 13160 | Bulk Product 908dbdf5 | Books | $999.75 | 169 | ★ 3.5 | 33 | BULK-1d5bedcf |
| 13161 | Bulk Product 8438e151 | Books | $528.36 | 487 | ★ 1.9 | 46 | BULK-d9e4f562 |
| 13162 | Bulk Product 468b948d | Sports & Outdoors | $76.30 | 438 | ★ 4.0 | 58 | BULK-cf5de53e |
| 13163 | Bulk Product c6908ce6 | Clothing | $396.36 | 336 | ★ 1.4 | 17 | BULK-b0970a03 |
| 13164 | Bulk Product f932d2f3 | Sports & Outdoors | $844.91 | 486 | ★ 2.7 | 88 | BULK-e2b97cf0 |
| 13165 | Bulk Product 36e9452b | Electronics | $732.19 | 248 | ★ 0.9 | 90 | BULK-feb84d41 |
| 13166 | Bulk Product 6e954793 | Electronics | $656.56 | 161 | ★ 5.0 | 29 | BULK-c1c36e3b |
| 13167 | Bulk Product 42dfee55 | Electronics | $701.61 | 43 | ★ 4.9 | 88 | BULK-a33daf19 |
| 13168 | Bulk Product 1df13faf | Sports & Outdoors | $54.96 | 301 | ★ 4.8 | 50 | BULK-4efd2cf8 |
| 13169 | Bulk Product 50de5bf7 | Home & Garden | $589.57 | 325 | ★ 3.8 | 15 | BULK-66c6bd00 |
| 13170 | Bulk Product d599be01 | Home & Garden | $13.68 | 395 | ★ 4.8 | 90 | BULK-74fc9bc8 |
| 13171 | Bulk Product 10578918 | Books | $416.54 | 291 | ★ 1.3 | 84 | BULK-8357f326 |
| 13172 | Bulk Product e61c4c65 | Toys & Games | $909.18 | 100 | ★ 2.6 | 94 | BULK-c2345abd |
| 13173 | Bulk Product 3307955c | Electronics | $239.67 | 318 | ★ 0.4 | 58 | BULK-dd868109 |
| 13174 | Bulk Product d95e5378 | Electronics | $112.75 | 351 | ★ 0.5 | 45 | BULK-893bc262 |
| 13175 | Bulk Product 1a0259c3 | Electronics | $48.35 | 392 | ★ 1.9 | 22 | BULK-ddbab937 |
| 13176 | Bulk Product 04742c33 | Books | $311.71 | 187 | ★ 0.4 | 38 | BULK-0790863b |
| 13177 | Bulk Product ae4d6f82 | Clothing | $683.42 | 261 | ★ 4.4 | 39 | BULK-29570f72 |
| 13178 | Bulk Product 2e6117b4 | Clothing | $920.68 | 459 | ★ 0.4 | 60 | BULK-e1419189 |
| 13179 | Bulk Product 9b26f9e9 | Sports & Outdoors | $562.93 | 307 | ★ 1.7 | 77 | BULK-cbcaa6e0 |
| 13180 | Bulk Product 67f11eb8 | Electronics | $644.31 | 188 | ★ 1.8 | 21 | BULK-7822e8a7 |
| 13181 | Bulk Product 390d3fe7 | Clothing | $932.08 | 492 | ★ 3.6 | 91 | BULK-db724a78 |
| 13182 | Bulk Product 631f49e7 | Toys & Games | $514.23 | 330 | ★ 4.6 | 44 | BULK-e31386da |
| 13183 | Bulk Product c3cc2a5a | Home & Garden | $326.73 | 103 | ★ 0.7 | 12 | BULK-a46f8bfa |
| 13184 | Bulk Product bd6e3644 | Clothing | $407.40 | 40 | ★ 1.1 | 73 | BULK-6bb70868 |
// 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
};
}