Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 981 | Mega Health & Beauty Product Advanced | Health & Beauty | $124.65 | 263 | ★ 4.1 | 837 | SKU-000981 |
| 982 | Deluxe Food & Grocery Product Advanced | Food & Grocery | $313.53 | 350 | ★ 4.3 | 883 | SKU-000982 |
| 983 | Standard Home & Garden Product XL | Home & Garden | $882.82 | 197 | ★ 3.3 | 961 | SKU-000983 |
| 984 | Super Sports & Outdoors Product Pro | Sports & Outdoors | $164.50 | 489 | ★ 3.8 | 731 | SKU-000984 |
| 985 | Mega Food & Grocery Product Mini | Food & Grocery | $82.68 | 142 | ★ 4.9 | 934 | SKU-000985 |
| 987 | Deluxe Clothing Product 2024 | Clothing | $255.76 | 150 | ★ 4.2 | 169 | SKU-000987 |
| 988 | Premium Health & Beauty Product Mini | Health & Beauty | $26.01 | 321 | ★ 3.8 | 960 | SKU-000988 |
| 989 | Professional Office Supplies Product Max | Office Supplies | $80.55 | 218 | ★ 2.2 | 702 | SKU-000989 |
| 990 | Pro Automotive Product Special Edition | Automotive | $894.25 | 309 | ★ 1.6 | 798 | SKU-000990 |
| 991 | Professional Health & Beauty Product Mini | Health & Beauty | $659.15 | 92 | ★ 2.8 | 588 | SKU-000991 |
| 992 | Deluxe Electronics Product Lite | Electronics | $484.22 | 283 | ★ 1.1 | 27 | SKU-000992 |
| 993 | Premium Clothing Product Lite | Clothing | $581.44 | 139 | ★ 3.1 | 287 | SKU-000993 |
| 994 | Pro Office Supplies Product Lite | Office Supplies | $168.65 | 452 | ★ 1.7 | 190 | SKU-000994 |
| 995 | Economy Automotive Product Classic | Automotive | $13.12 | 434 | ★ 3.8 | 931 | SKU-000995 |
| 997 | Premium Automotive Product Lite | Automotive | $119.93 | 347 | ★ 3.3 | 107 | SKU-000997 |
| 998 | Professional Food & Grocery Product Plus | Food & Grocery | $581.31 | 316 | ★ 3.2 | 768 | SKU-000998 |
| 999 | Professional Food & Grocery Product Lite | Food & Grocery | $631.66 | 300 | ★ 4.7 | 524 | SKU-000999 |
| 1000 | Economy Food & Grocery Product Special Edition | Food & Grocery | $846.27 | 186 | ★ 4.3 | 980 | SKU-001000 |
| 1001 | Economy Office Supplies Product Special Edition | Office Supplies | $773.78 | 375 | ★ 1.9 | 572 | SKU-001001 |
| 1002 | Deluxe Health & Beauty Product Advanced | Health & Beauty | $957.81 | 368 | ★ 2.9 | 835 | SKU-001002 |
| 1003 | Economy Clothing Product Lite | Clothing | $113.78 | 410 | ★ 3.4 | 779 | SKU-001003 |
| 1004 | Economy Books Product Lite | Books | $596.95 | 411 | ★ 1.8 | 344 | SKU-001004 |
| 1005 | Economy Office Supplies Product Plus | Office Supplies | $319.10 | 261 | ★ 1.8 | 223 | SKU-001005 |
| 1007 | Super Home & Garden Product Plus | Home & Garden | $531.19 | 251 | ★ 2.0 | 572 | SKU-001007 |
| 1008 | Deluxe Office Supplies Product Classic | Office Supplies | $580.50 | 230 | ★ 4.2 | 418 | SKU-001008 |
// 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
};
}