Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 11210 | Bulk Product f2f9f77c | Books | $822.36 | 160 | ★ 2.1 | 55 | BULK-d3ec82e5 |
| 11211 | Bulk Product 4e599ee9 | Toys & Games | $116.73 | 335 | ★ 1.3 | 65 | BULK-972a266f |
| 11212 | Bulk Product 1545fe50 | Toys & Games | $958.74 | 98 | ★ 4.6 | 88 | BULK-79b03cbd |
| 11213 | Bulk Product 3873af37 | Books | $864.98 | 353 | ★ 0.9 | 24 | BULK-effe4d5a |
| 11214 | Bulk Product 12afc0e2 | Books | $857.76 | 89 | ★ 4.7 | 7 | BULK-c9b10f70 |
| 11215 | Bulk Product 242edaa8 | Electronics | $86.19 | 74 | ★ 2.1 | 86 | BULK-e2b4dfe1 |
| 11216 | Bulk Product e858dfd1 | Electronics | $756.49 | 50 | ★ 0.8 | 64 | BULK-ef8c37b8 |
| 11217 | Bulk Product 14f8c9d6 | Home & Garden | $697.01 | 122 | ★ 4.4 | 86 | BULK-e99bd4cf |
| 11218 | Bulk Product eeaa651e | Clothing | $399.28 | 229 | ★ 1.3 | 80 | BULK-d5d06c86 |
| 11219 | Bulk Product 221ac417 | Home & Garden | $202.30 | 303 | ★ 3.9 | 29 | BULK-05f10c0f |
| 11220 | Bulk Product 905d7361 | Clothing | $595.81 | 346 | ★ 1.7 | 20 | BULK-8bfa8f83 |
| 11221 | Bulk Product c97d873e | Toys & Games | $423.11 | 115 | ★ 4.6 | 91 | BULK-96cee8dd |
| 11222 | Bulk Product b7797880 | Toys & Games | $472.66 | 398 | ★ 3.7 | 76 | BULK-d6eb0181 |
| 11223 | Bulk Product 8fa9399b | Sports & Outdoors | $257.49 | 212 | ★ 1.8 | 41 | BULK-c6045126 |
| 11224 | Bulk Product bf8ee326 | Books | $545.11 | 293 | ★ 3.8 | 66 | BULK-8e9dd87a |
| 11225 | Bulk Product abcdf82a | Books | $833.17 | 334 | ★ 3.8 | 80 | BULK-3da55eca |
| 11226 | Bulk Product 455febf4 | Books | $121.54 | 357 | ★ 2.1 | 39 | BULK-0b8085f6 |
| 11227 | Bulk Product ca442415 | Books | $542.24 | 242 | ★ 3.8 | 24 | BULK-2e659960 |
| 11228 | Bulk Product c5f18650 | Toys & Games | $32.02 | 19 | ★ 0.2 | 49 | BULK-1fd1e3b8 |
| 11229 | Bulk Product 38c04735 | Home & Garden | $142.28 | 241 | ★ 2.8 | 96 | BULK-bf085e8d |
| 11230 | Bulk Product f7e13013 | Books | $305.44 | 299 | ★ 1.7 | 83 | BULK-90e17d5d |
| 11231 | Bulk Product 906ba399 | Home & Garden | $370.61 | 15 | ★ 3.4 | 68 | BULK-9c82186c |
| 11232 | Bulk Product b9d39eb8 | Toys & Games | $523.04 | 165 | ★ 2.6 | 4 | BULK-14b7b640 |
| 11233 | Bulk Product b45fe8e0 | Toys & Games | $928.33 | 7 | ★ 4.5 | 50 | BULK-65dcb732 |
| 11234 | Bulk Product 25a83968 | Books | $802.57 | 10 | ★ 0.5 | 97 | BULK-30f5515a |
// 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
};
}