Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 194 | Economy Electronics Product Classic | Electronics | $985.98 | 257 | ★ 3.2 | 976 | SKU-000194 |
| 195 | Standard Toys & Games Product Classic | Toys & Games | $85.92 | 113 | ★ 3.2 | 602 | SKU-000195 |
| 198 | Ultra Sports & Outdoors Product XL | Sports & Outdoors | $533.37 | 313 | ★ 4.4 | 198 | SKU-000198 |
| 199 | Pro Clothing Product Mini | Clothing | $612.01 | 404 | ★ 2.3 | 335 | SKU-000199 |
| 200 | Economy Books Product Mini | Books | $937.23 | 208 | ★ 2.4 | 851 | SKU-000200 |
| 201 | Deluxe Automotive Product Classic | Automotive | $194.53 | 115 | ★ 3.6 | 374 | SKU-000201 |
| 202 | Premium Health & Beauty Product XL | Health & Beauty | $906.93 | 39 | ★ 3.5 | 594 | SKU-000202 |
| 203 | Super Automotive Product XL | Automotive | $123.27 | 92 | ★ 4.6 | 123 | SKU-000203 |
| 204 | Economy Electronics Product XL | Electronics | $193.57 | 37 | ★ 1.2 | 291 | SKU-000204 |
| 206 | Super Clothing Product Advanced | Clothing | $131.15 | 329 | ★ 4.8 | 94 | SKU-000206 |
| 207 | Elite Automotive Product Special Edition | Automotive | $751.26 | 148 | ★ 3.9 | 353 | SKU-000207 |
| 208 | Super Toys & Games Product Plus | Toys & Games | $504.30 | 115 | ★ 4.4 | 361 | SKU-000208 |
| 209 | Economy Automotive Product XL | Automotive | $803.34 | 144 | ★ 2.4 | 981 | SKU-000209 |
| 210 | Deluxe Electronics Product Special Edition | Electronics | $765.60 | 68 | ★ 1.4 | 655 | SKU-000210 |
| 211 | Deluxe Books Product Mini | Books | $830.74 | 476 | ★ 4.8 | 182 | SKU-000211 |
| 212 | Ultra Automotive Product Mini | Automotive | $608.05 | 55 | ★ 4.4 | 840 | SKU-000212 |
| 213 | Deluxe Food & Grocery Product XL | Food & Grocery | $437.55 | 284 | ★ 4.3 | 696 | SKU-000213 |
| 214 | Premium Books Product Mini | Books | $478.55 | 297 | ★ 3.9 | 965 | SKU-000214 |
| 215 | Standard Toys & Games Product 2024 | Toys & Games | $519.39 | 291 | ★ 2.4 | 17 | SKU-000215 |
| 216 | Super Books Product Mini | Books | $520.59 | 137 | ★ 4.6 | 504 | SKU-000216 |
| 217 | Ultra Clothing Product 2024 | Clothing | $719.18 | 318 | ★ 4.8 | 557 | SKU-000217 |
| 218 | Standard Office Supplies Product Special Edition | Office Supplies | $23.41 | 203 | ★ 3.5 | 684 | SKU-000218 |
| 219 | Economy Clothing Product Classic | Clothing | $525.03 | 482 | ★ 3.9 | 905 | SKU-000219 |
| 220 | Super Toys & Games Product Plus | Toys & Games | $587.39 | 460 | ★ 4.7 | 274 | SKU-000220 |
| 221 | Standard Clothing Product Lite | Clothing | $236.56 | 63 | ★ 4.4 | 765 | SKU-000221 |
// 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
};
}