Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 13935 | Bulk Product 8abc2f9e | Home & Garden | $543.94 | 110 | ★ 3.9 | 83 | BULK-dab28b42 |
| 13936 | Bulk Product 4c7df935 | Toys & Games | $848.47 | 188 | ★ 3.9 | 36 | BULK-02d149f8 |
| 13937 | Bulk Product 9a64d494 | Sports & Outdoors | $999.95 | 374 | ★ 1.1 | 66 | BULK-49fcefc0 |
| 13938 | Bulk Product 9e24821d | Electronics | $892.85 | 107 | ★ 2.7 | 71 | BULK-9339ca01 |
| 13939 | Bulk Product 82cad23b | Electronics | $576.67 | 24 | ★ 2.5 | 52 | BULK-ab0db631 |
| 13940 | Bulk Product 2ea0b8aa | Electronics | $131.72 | 270 | ★ 2.1 | 99 | BULK-d11d021c |
| 13941 | Bulk Product 086ab1e5 | Books | $324.36 | 155 | ★ 5.0 | 56 | BULK-965c3453 |
| 13942 | Bulk Product 084ffa06 | Sports & Outdoors | $927.30 | 386 | ★ 3.1 | 2 | BULK-1bc4d433 |
| 13943 | Bulk Product a1ad7f5a | Electronics | $576.11 | 107 | ★ 4.7 | 88 | BULK-3167239a |
| 13944 | Bulk Product 3c00a36d | Sports & Outdoors | $78.82 | 135 | ★ 4.4 | 51 | BULK-42c554d6 |
| 13945 | Bulk Product dc17fedf | Electronics | $298.41 | 65 | ★ 2.8 | 73 | BULK-18570bce |
| 13946 | Bulk Product da5ca854 | Books | $777.71 | 492 | ★ 3.8 | 9 | BULK-e195e22c |
| 13947 | Bulk Product 604d6cbd | Toys & Games | $234.55 | 431 | ★ 4.9 | 83 | BULK-2237e30c |
| 13948 | Bulk Product d851f213 | Clothing | $583.78 | 85 | ★ 3.8 | 70 | BULK-8b48dcf8 |
| 13949 | Bulk Product e05bbd4a | Clothing | $363.64 | 314 | ★ 4.5 | 92 | BULK-6decd668 |
| 13950 | Bulk Product 6c10e359 | Toys & Games | $311.69 | 55 | ★ 1.5 | 77 | BULK-6c8da6cf |
| 13951 | Bulk Product aa5e8f8d | Home & Garden | $108.27 | 297 | ★ 1.2 | 17 | BULK-3c17ab66 |
| 13952 | Bulk Product 51726a40 | Toys & Games | $305.05 | 252 | ★ 3.7 | 92 | BULK-c4e5b204 |
| 13953 | Bulk Product b5fcf3a4 | Sports & Outdoors | $526.41 | 461 | ★ 2.1 | 55 | BULK-0cdd5d23 |
| 13954 | Bulk Product 31c3213f | Toys & Games | $598.47 | 157 | ★ 4.9 | 79 | BULK-21bd1051 |
| 13955 | Bulk Product 82d5a785 | Sports & Outdoors | $280.55 | 214 | ★ 1.5 | 34 | BULK-e90ae8c2 |
| 13956 | Bulk Product 98871844 | Electronics | $62.77 | 15 | ★ 0.0 | 47 | BULK-6ffb4567 |
| 13957 | Bulk Product d7ac0dd1 | Sports & Outdoors | $399.98 | 16 | ★ 3.8 | 34 | BULK-41c0ba4c |
| 13958 | Bulk Product ba8b7c8a | Home & Garden | $608.21 | 386 | ★ 4.3 | 55 | BULK-60feeb52 |
| 13959 | Bulk Product cff236bd | Electronics | $34.00 | 97 | ★ 4.7 | 92 | BULK-0daea6e7 |
// 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
};
}