Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 1245 | Ultra Home & Garden Product 2024 | Home & Garden | $115.12 | 28 | ★ 4.9 | 877 | SKU-001245 |
| 1246 | Elite Health & Beauty Product XL | Health & Beauty | $321.91 | 92 | ★ 4.1 | 210 | SKU-001246 |
| 1247 | Mega Home & Garden Product Classic | Home & Garden | $802.69 | 9 | ★ 3.0 | 670 | SKU-001247 |
| 1248 | Standard Electronics Product Mini | Electronics | $727.55 | 85 | ★ 1.6 | 741 | SKU-001248 |
| 1249 | Ultra Home & Garden Product Advanced | Home & Garden | $611.68 | 51 | ★ 4.6 | 38 | SKU-001249 |
| 1250 | Professional Clothing Product Max | Clothing | $831.98 | 497 | ★ 3.9 | 251 | SKU-001250 |
| 1251 | Ultra Food & Grocery Product 2024 | Food & Grocery | $306.33 | 238 | ★ 1.5 | 611 | SKU-001251 |
| 1252 | Deluxe Books Product Advanced | Books | $288.50 | 191 | ★ 3.8 | 122 | SKU-001252 |
| 1254 | Ultra Clothing Product XL | Clothing | $417.67 | 475 | ★ 1.7 | 747 | SKU-001254 |
| 1256 | Pro Health & Beauty Product XL | Health & Beauty | $175.16 | 195 | ★ 3.3 | 413 | SKU-001256 |
| 1257 | Economy Sports & Outdoors Product Max | Sports & Outdoors | $732.81 | 120 | ★ 3.1 | 752 | SKU-001257 |
| 1259 | Pro Books Product Classic | Books | $673.28 | 105 | ★ 3.9 | 737 | SKU-001259 |
| 1260 | Economy Home & Garden Product Advanced | Home & Garden | $806.58 | 109 | ★ 3.6 | 220 | SKU-001260 |
| 1261 | Ultra Health & Beauty Product Pro | Health & Beauty | $694.71 | 315 | ★ 2.2 | 843 | SKU-001261 |
| 1262 | Mega Books Product Plus | Books | $943.40 | 93 | ★ 1.5 | 326 | SKU-001262 |
| 1263 | Elite Toys & Games Product Advanced | Toys & Games | $998.91 | 347 | ★ 4.6 | 804 | SKU-001263 |
| 1264 | Ultra Automotive Product Plus | Automotive | $379.27 | 263 | ★ 4.2 | 48 | SKU-001264 |
| 1265 | Economy Health & Beauty Product XL | Health & Beauty | $107.01 | 335 | ★ 1.7 | 343 | SKU-001265 |
| 1267 | Deluxe Food & Grocery Product XL | Food & Grocery | $679.81 | 327 | ★ 2.7 | 367 | SKU-001267 |
| 1268 | Professional Clothing Product Lite | Clothing | $403.04 | 423 | ★ 4.0 | 159 | SKU-001268 |
| 1269 | Professional Clothing Product Special Edition | Clothing | $768.97 | 314 | ★ 2.8 | 431 | SKU-001269 |
| 1270 | Professional Automotive Product Advanced | Automotive | $737.85 | 233 | ★ 3.6 | 932 | SKU-001270 |
| 1271 | Standard Automotive Product Classic | Automotive | $445.32 | 201 | ★ 1.9 | 18 | SKU-001271 |
| 1272 | Ultra Home & Garden Product XL | Home & Garden | $411.19 | 283 | ★ 1.7 | 401 | SKU-001272 |
| 1273 | Super Toys & Games Product 2024 | Toys & Games | $509.49 | 427 | ★ 3.3 | 225 | SKU-001273 |
// 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
};
}