Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 756 | Professional Food & Grocery Product XL | Food & Grocery | $915.47 | 355 | ★ 2.1 | 458 | SKU-000756 |
| 757 | Mega Toys & Games Product XL | Toys & Games | $34.26 | 22 | ★ 1.6 | 714 | SKU-000757 |
| 758 | Super Books Product Lite | Books | $288.77 | 97 | ★ 4.0 | 752 | SKU-000758 |
| 759 | Deluxe Clothing Product Special Edition | Clothing | $556.44 | 223 | ★ 4.6 | 653 | SKU-000759 |
| 760 | Deluxe Automotive Product Special Edition | Automotive | $569.15 | 112 | ★ 1.2 | 35 | SKU-000760 |
| 761 | Standard Food & Grocery Product Advanced | Food & Grocery | $54.26 | 454 | ★ 4.6 | 885 | SKU-000761 |
| 762 | Super Food & Grocery Product 2024 | Food & Grocery | $412.20 | 24 | ★ 3.2 | 578 | SKU-000762 |
| 763 | Premium Clothing Product Lite | Clothing | $881.56 | 156 | ★ 3.3 | 61 | SKU-000763 |
| 764 | Deluxe Automotive Product Plus | Automotive | $246.78 | 165 | ★ 4.7 | 77 | SKU-000764 |
| 765 | Economy Health & Beauty Product Plus | Health & Beauty | $409.97 | 434 | ★ 1.1 | 777 | SKU-000765 |
| 766 | Standard Automotive Product Special Edition | Automotive | $905.73 | 474 | ★ 3.5 | 76 | SKU-000766 |
| 767 | Economy Clothing Product Plus | Clothing | $386.34 | 104 | ★ 4.9 | 387 | SKU-000767 |
| 768 | Elite Sports & Outdoors Product XL | Sports & Outdoors | $283.12 | 257 | ★ 4.2 | 520 | SKU-000768 |
| 769 | Economy Clothing Product 2024 | Clothing | $474.44 | 283 | ★ 2.3 | 440 | SKU-000769 |
| 770 | Professional Books Product Special Edition | Books | $930.22 | 488 | ★ 2.2 | 582 | SKU-000770 |
| 771 | Standard Automotive Product Pro | Automotive | $336.73 | 380 | ★ 2.3 | 931 | SKU-000771 |
| 773 | Mega Clothing Product Plus | Clothing | $962.22 | 222 | ★ 3.4 | 363 | SKU-000773 |
| 774 | Professional Electronics Product Pro | Electronics | $219.68 | 318 | ★ 3.7 | 948 | SKU-000774 |
| 775 | Elite Home & Garden Product Lite | Home & Garden | $275.64 | 401 | ★ 2.9 | 640 | SKU-000775 |
| 776 | Super Home & Garden Product Mini | Home & Garden | $875.13 | 18 | ★ 3.2 | 939 | SKU-000776 |
| 777 | Pro Toys & Games Product Mini | Toys & Games | $638.31 | 338 | ★ 3.4 | 330 | SKU-000777 |
| 778 | Premium Food & Grocery Product XL | Food & Grocery | $259.19 | 170 | ★ 4.7 | 784 | SKU-000778 |
| 780 | Professional Toys & Games Product Lite | Toys & Games | $621.92 | 198 | ★ 3.8 | 629 | SKU-000780 |
| 781 | Elite Home & Garden Product Mini | Home & Garden | $724.33 | 28 | ★ 1.6 | 911 | SKU-000781 |
| 782 | Economy Food & Grocery Product Plus | Food & Grocery | $836.40 | 66 | ★ 1.4 | 719 | SKU-000782 |
// 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
};
}