Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 85 | Pro Clothing Product 2024 | Clothing | $480.82 | 154 | ★ 2.4 | 538 | SKU-000085 |
| 87 | Standard Electronics Product Classic | Electronics | $427.87 | 158 | ★ 3.6 | 112 | SKU-000087 |
| 88 | Super Sports & Outdoors Product Mini | Sports & Outdoors | $788.87 | 280 | ★ 4.9 | 957 | SKU-000088 |
| 89 | Ultra Clothing Product 2024 | Clothing | $624.13 | 322 | ★ 2.2 | 487 | SKU-000089 |
| 90 | Professional Books Product Max | Books | $404.66 | 27 | ★ 3.3 | 380 | SKU-000090 |
| 91 | Super Books Product Advanced | Books | $760.07 | 118 | ★ 2.3 | 319 | SKU-000091 |
| 93 | Pro Books Product Classic | Books | $226.12 | 337 | ★ 3.0 | 359 | SKU-000093 |
| 95 | Economy Automotive Product Pro | Automotive | $585.14 | 115 | ★ 1.6 | 802 | SKU-000095 |
| 96 | Mega Health & Beauty Product Plus | Health & Beauty | $430.60 | 252 | ★ 3.8 | 307 | SKU-000096 |
| 97 | Pro Home & Garden Product 2024 | Home & Garden | $236.59 | 50 | ★ 1.8 | 575 | SKU-000097 |
| 98 | Professional Clothing Product Special Edition | Clothing | $655.24 | 163 | ★ 4.9 | 86 | SKU-000098 |
| 99 | Elite Health & Beauty Product Max | Health & Beauty | $599.69 | 317 | ★ 2.7 | 125 | SKU-000099 |
| 100 | Super Home & Garden Product Plus | Home & Garden | $883.39 | 321 | ★ 4.8 | 995 | SKU-000100 |
| 101 | Deluxe Health & Beauty Product Special Edition | Health & Beauty | $63.90 | 183 | ★ 1.1 | 364 | SKU-000101 |
| 102 | Standard Books Product Mini | Books | $451.17 | 476 | ★ 1.9 | 503 | SKU-000102 |
| 104 | Deluxe Sports & Outdoors Product Plus | Sports & Outdoors | $110.96 | 257 | ★ 2.2 | 974 | SKU-000104 |
| 105 | Mega Electronics Product Classic | Electronics | $917.76 | 483 | ★ 3.3 | 952 | SKU-000105 |
| 106 | Mega Sports & Outdoors Product Advanced | Sports & Outdoors | $412.27 | 24 | ★ 4.0 | 805 | SKU-000106 |
| 107 | Pro Home & Garden Product Special Edition | Home & Garden | $395.62 | 270 | ★ 2.4 | 857 | SKU-000107 |
| 108 | Pro Automotive Product Plus | Automotive | $85.14 | 148 | ★ 2.2 | 711 | SKU-000108 |
| 109 | Professional Clothing Product Pro | Clothing | $423.88 | 273 | ★ 4.2 | 164 | SKU-000109 |
| 110 | Standard Home & Garden Product Classic | Home & Garden | $789.99 | 494 | ★ 1.0 | 309 | SKU-000110 |
| 111 | Deluxe Health & Beauty Product Plus | Health & Beauty | $45.68 | 204 | ★ 4.9 | 458 | SKU-000111 |
| 112 | Super Electronics Product Special Edition | Electronics | $588.99 | 226 | ★ 3.9 | 334 | SKU-000112 |
| 113 | Economy Sports & Outdoors Product Pro | Sports & Outdoors | $893.91 | 358 | ★ 1.3 | 253 | SKU-000113 |
// 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
};
}