Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 23185 | Bulk Product 2bed42ba | Clothing | $470.39 | 388 | ★ 3.5 | 12 | BULK-c42d59d6 |
| 23186 | Bulk Product a2780aec | Home & Garden | $686.14 | 5 | ★ 2.1 | 14 | BULK-2462c4bb |
| 23187 | Bulk Product 121a822c | Books | $108.28 | 391 | ★ 0.0 | 33 | BULK-5aef2528 |
| 23188 | Bulk Product fe93df36 | Toys & Games | $658.08 | 433 | ★ 4.2 | 49 | BULK-a7122441 |
| 23189 | Bulk Product e2fe900e | Sports & Outdoors | $665.15 | 57 | ★ 4.1 | 55 | BULK-6ccfa509 |
| 23190 | Bulk Product a08358e7 | Home & Garden | $626.32 | 277 | ★ 3.9 | 31 | BULK-e6d02ee2 |
| 23191 | Bulk Product ed28605a | Home & Garden | $756.38 | 190 | ★ 1.3 | 84 | BULK-01611a27 |
| 23192 | Bulk Product a13f6c03 | Home & Garden | $810.02 | 133 | ★ 1.3 | 35 | BULK-2a40b5f8 |
| 23193 | Bulk Product 1dfebcbc | Books | $339.72 | 448 | ★ 3.3 | 87 | BULK-98159e33 |
| 23194 | Bulk Product 458ceb80 | Toys & Games | $711.23 | 172 | ★ 1.0 | 81 | BULK-1715e3c5 |
| 23195 | Bulk Product 8f2c72ce | Sports & Outdoors | $952.20 | 189 | ★ 2.1 | 76 | BULK-9fbf5873 |
| 23196 | Bulk Product 13eab8bc | Toys & Games | $167.74 | 80 | ★ 0.8 | 41 | BULK-bdf77415 |
| 23197 | Bulk Product d0e8bd2b | Home & Garden | $456.44 | 9 | ★ 2.7 | 13 | BULK-81b82a21 |
| 23198 | Bulk Product e9d665a3 | Toys & Games | $504.83 | 85 | ★ 2.9 | 97 | BULK-16dd6496 |
| 23199 | Bulk Product 503fa9c7 | Toys & Games | $619.72 | 202 | ★ 3.3 | 10 | BULK-177412d3 |
| 23200 | Bulk Product 29b410fd | Electronics | $982.45 | 294 | ★ 2.3 | 11 | BULK-2ba7b266 |
| 23201 | Bulk Product c2aa8abb | Toys & Games | $455.00 | 81 | ★ 0.0 | 94 | BULK-8b9d2ed7 |
| 23202 | Bulk Product eac53520 | Sports & Outdoors | $779.70 | 263 | ★ 1.3 | 60 | BULK-bae22f1d |
| 23203 | Bulk Product 418b3cea | Toys & Games | $965.83 | 365 | ★ 1.6 | 15 | BULK-8074d2bc |
| 23204 | Bulk Product 652939b0 | Home & Garden | $847.29 | 304 | ★ 2.1 | 68 | BULK-7e32a8d4 |
| 23205 | Bulk Product 3a33b684 | Books | $292.36 | 66 | ★ 3.8 | 98 | BULK-1c40a7fb |
| 23206 | Bulk Product 4fe6eac9 | Electronics | $790.18 | 98 | ★ 1.0 | 93 | BULK-29ee879d |
| 23207 | Bulk Product 0b9c77d3 | Sports & Outdoors | $900.35 | 460 | ★ 1.7 | 3 | BULK-33ad37aa |
| 23208 | Bulk Product 622f2986 | Electronics | $844.29 | 490 | ★ 0.3 | 65 | BULK-9ad55fa4 |
| 23209 | Bulk Product c8381881 | Books | $634.88 | 446 | ★ 1.4 | 9 | BULK-d7f4a871 |
// 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
};
}