Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 21010 | Bulk Product 64e0cac4 | Clothing | $828.97 | 429 | ★ 2.6 | 5 | BULK-2b5734af |
| 21011 | Bulk Product 41bd9d91 | Toys & Games | $703.30 | 406 | ★ 3.5 | 18 | BULK-8c689ed1 |
| 21012 | Bulk Product 6b2e46bd | Electronics | $143.18 | 99 | ★ 2.9 | 72 | BULK-f55f1378 |
| 21013 | Bulk Product fa6ea93a | Home & Garden | $711.72 | 139 | ★ 3.0 | 65 | BULK-2134f972 |
| 21014 | Bulk Product c8054548 | Clothing | $60.43 | 171 | ★ 1.3 | 42 | BULK-5bfb822a |
| 21015 | Bulk Product d1f65259 | Home & Garden | $292.31 | 470 | ★ 2.9 | 8 | BULK-cccd6443 |
| 21016 | Bulk Product 1a2e5926 | Electronics | $544.02 | 101 | ★ 4.0 | 90 | BULK-f70201e9 |
| 21017 | Bulk Product c1e4c7c0 | Home & Garden | $58.70 | 447 | ★ 2.0 | 39 | BULK-bd3cee52 |
| 21018 | Bulk Product 30c49f41 | Clothing | $1,009.03 | 377 | ★ 0.4 | 75 | BULK-375b5ca2 |
| 21019 | Bulk Product b4921e17 | Home & Garden | $690.15 | 380 | ★ 2.3 | 32 | BULK-648e0769 |
| 21020 | Bulk Product 03fbd7b6 | Electronics | $546.45 | 189 | ★ 0.0 | 31 | BULK-e295f0fb |
| 21021 | Bulk Product 2bcef65b | Toys & Games | $978.44 | 410 | ★ 0.6 | 35 | BULK-06da5996 |
| 21022 | Bulk Product 5f88bb10 | Sports & Outdoors | $190.65 | 362 | ★ 3.5 | 84 | BULK-8307595c |
| 21023 | Bulk Product c9cf6a10 | Clothing | $292.33 | 69 | ★ 3.8 | 25 | BULK-903c65bf |
| 21024 | Bulk Product 2aedfe38 | Sports & Outdoors | $615.30 | 264 | ★ 2.9 | 66 | BULK-e991741f |
| 21025 | Bulk Product 0bfc2199 | Home & Garden | $979.18 | 124 | ★ 2.0 | 78 | BULK-3a2b035b |
| 21026 | Bulk Product bf636d34 | Sports & Outdoors | $925.92 | 387 | ★ 0.9 | 79 | BULK-db226dfb |
| 21027 | Bulk Product 6f89947f | Books | $915.02 | 375 | ★ 3.9 | 99 | BULK-f93c0844 |
| 21028 | Bulk Product a80f2a55 | Books | $652.26 | 26 | ★ 1.6 | 85 | BULK-75a96c03 |
| 21029 | Bulk Product 40b2982e | Sports & Outdoors | $490.23 | 129 | ★ 2.3 | 86 | BULK-c1cea1fb |
| 21030 | Bulk Product d6513196 | Clothing | $456.85 | 345 | ★ 4.0 | 43 | BULK-14e4b533 |
| 21031 | Bulk Product 8c148274 | Electronics | $93.40 | 164 | ★ 3.4 | 50 | BULK-eeca860b |
| 21032 | Bulk Product 2c7c6bf9 | Books | $646.70 | 453 | ★ 4.2 | 99 | BULK-a951093d |
| 21033 | Bulk Product 33d3518c | Clothing | $947.92 | 426 | ★ 2.1 | 49 | BULK-be8e9fe0 |
| 21034 | Bulk Product 2f3467af | Sports & Outdoors | $602.24 | 486 | ★ 2.8 | 71 | BULK-b37fffde |
// 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
};
}