Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 3282 | Deluxe Electronics Product Special Edition | Electronics | $55.78 | 388 | ★ 2.1 | 455 | SKU-003282 |
| 3283 | Super Automotive Product 2024 | Automotive | $422.28 | 108 | ★ 2.6 | 998 | SKU-003283 |
| 3286 | Elite Toys & Games Product Mini | Toys & Games | $878.34 | 256 | ★ 4.6 | 803 | SKU-003286 |
| 3288 | Super Books Product XL | Books | $145.82 | 378 | ★ 2.6 | 112 | SKU-003288 |
| 3289 | Deluxe Clothing Product Classic | Clothing | $241.36 | 317 | ★ 3.7 | 279 | SKU-003289 |
| 3290 | Super Electronics Product Pro | Electronics | $532.98 | 482 | ★ 3.3 | 146 | SKU-003290 |
| 3292 | Pro Clothing Product Mini | Clothing | $341.52 | 3 | ★ 1.6 | 269 | SKU-003292 |
| 3293 | Standard Books Product Special Edition | Books | $479.18 | 481 | ★ 1.1 | 722 | SKU-003293 |
| 3294 | Ultra Health & Beauty Product Advanced | Health & Beauty | $33.92 | 419 | ★ 3.4 | 626 | SKU-003294 |
| 3295 | Super Books Product Mini | Books | $593.78 | 148 | ★ 1.7 | 595 | SKU-003295 |
| 3296 | Deluxe Food & Grocery Product Special Edition | Food & Grocery | $617.12 | 221 | ★ 3.9 | 337 | SKU-003296 |
| 3298 | Elite Toys & Games Product Plus | Toys & Games | $152.75 | 331 | ★ 3.9 | 288 | SKU-003298 |
| 3299 | Deluxe Food & Grocery Product Classic | Food & Grocery | $991.61 | 188 | ★ 3.9 | 516 | SKU-003299 |
| 3301 | Economy Books Product Classic | Books | $289.75 | 203 | ★ 5.0 | 417 | SKU-003301 |
| 3302 | Deluxe Books Product Special Edition | Books | $461.26 | 60 | ★ 2.5 | 360 | SKU-003302 |
| 3303 | Professional Home & Garden Product Special Edition | Home & Garden | $576.77 | 17 | ★ 4.0 | 427 | SKU-003303 |
| 3304 | Premium Books Product 2024 | Books | $470.55 | 196 | ★ 2.0 | 540 | SKU-003304 |
| 3305 | Elite Toys & Games Product Mini | Toys & Games | $56.70 | 42 | ★ 2.9 | 110 | SKU-003305 |
| 3306 | Ultra Automotive Product Advanced | Automotive | $650.91 | 80 | ★ 3.5 | 355 | SKU-003306 |
| 3307 | Standard Sports & Outdoors Product XL | Sports & Outdoors | $690.59 | 294 | ★ 1.8 | 144 | SKU-003307 |
| 3308 | Professional Office Supplies Product Max | Office Supplies | $563.75 | 193 | ★ 3.5 | 962 | SKU-003308 |
| 3309 | Mega Toys & Games Product Lite | Toys & Games | $210.07 | 160 | ★ 3.2 | 922 | SKU-003309 |
| 3310 | Deluxe Toys & Games Product Classic | Toys & Games | $606.41 | 473 | ★ 2.3 | 915 | SKU-003310 |
| 3311 | Professional Toys & Games Product Lite | Toys & Games | $368.86 | 178 | ★ 3.4 | 296 | SKU-003311 |
| 3312 | Premium Office Supplies Product Pro | Office Supplies | $197.91 | 334 | ★ 2.8 | 846 | SKU-003312 |
// 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
};
}