Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 143 | Pro Office Supplies Product Plus | Office Supplies | $144.88 | 28 | ★ 1.6 | 346 | SKU-000143 |
| 144 | Ultra Toys & Games Product 2024 | Toys & Games | $489.32 | 222 | ★ 4.7 | 467 | SKU-000144 |
| 145 | Elite Clothing Product Mini | Clothing | $339.70 | 32 | ★ 2.2 | 111 | SKU-000145 |
| 146 | Economy Office Supplies Product Special Edition | Office Supplies | $937.15 | 117 | ★ 2.1 | 465 | SKU-000146 |
| 147 | Mega Electronics Product XL | Electronics | $409.99 | 189 | ★ 2.5 | 643 | SKU-000147 |
| 148 | Elite Toys & Games Product Special Edition | Toys & Games | $905.51 | 430 | ★ 4.2 | 675 | SKU-000148 |
| 149 | Economy Office Supplies Product Pro | Office Supplies | $211.18 | 226 | ★ 3.8 | 292 | SKU-000149 |
| 150 | Economy Health & Beauty Product 2024 | Health & Beauty | $937.12 | 282 | ★ 4.5 | 997 | SKU-000150 |
| 151 | Premium Electronics Product Lite | Electronics | $283.39 | 184 | ★ 3.3 | 935 | SKU-000151 |
| 152 | Ultra Electronics Product XL | Electronics | $673.87 | 394 | ★ 3.4 | 272 | SKU-000152 |
| 153 | Deluxe Sports & Outdoors Product XL | Sports & Outdoors | $842.02 | 149 | ★ 4.1 | 836 | SKU-000153 |
| 154 | Premium Clothing Product Lite | Clothing | $178.37 | 442 | ★ 4.9 | 884 | SKU-000154 |
| 155 | Super Health & Beauty Product Max | Health & Beauty | $241.56 | 437 | ★ 3.7 | 686 | SKU-000155 |
| 156 | Economy Home & Garden Product Mini | Home & Garden | $178.20 | 49 | ★ 3.9 | 387 | SKU-000156 |
| 157 | Standard Health & Beauty Product 2024 | Health & Beauty | $763.11 | 425 | ★ 4.5 | 961 | SKU-000157 |
| 158 | Economy Home & Garden Product Classic | Home & Garden | $476.52 | 384 | ★ 1.5 | 941 | SKU-000158 |
| 159 | Premium Office Supplies Product Plus | Office Supplies | $829.99 | 71 | ★ 2.8 | 339 | SKU-000159 |
| 160 | Elite Electronics Product Pro | Electronics | $177.06 | 254 | ★ 4.1 | 585 | SKU-000160 |
| 161 | Pro Office Supplies Product Lite | Office Supplies | $878.46 | 279 | ★ 4.4 | 780 | SKU-000161 |
| 162 | Deluxe Sports & Outdoors Product Classic | Sports & Outdoors | $976.17 | 41 | ★ 3.3 | 257 | SKU-000162 |
| 163 | Ultra Health & Beauty Product Advanced | Health & Beauty | $203.69 | 115 | ★ 3.9 | 921 | SKU-000163 |
| 164 | Professional Automotive Product Lite | Automotive | $676.90 | 55 | ★ 3.6 | 169 | SKU-000164 |
| 165 | Mega Electronics Product Advanced | Electronics | $225.52 | 382 | ★ 2.1 | 859 | SKU-000165 |
| 166 | Ultra Books Product Max | Books | $850.39 | 202 | ★ 2.8 | 3 | SKU-000166 |
| 167 | Professional Electronics Product Classic | Electronics | $190.98 | 216 | ★ 4.7 | 708 | SKU-000167 |
// 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
};
}