Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 250 | Economy Food & Grocery Product Classic | Food & Grocery | $606.63 | 52 | ★ 3.3 | 328 | SKU-000250 |
| 251 | Elite Clothing Product Lite | Clothing | $632.61 | 55 | ★ 1.7 | 473 | SKU-000251 |
| 252 | Mega Food & Grocery Product XL | Food & Grocery | $499.64 | 127 | ★ 3.5 | 947 | SKU-000252 |
| 253 | Pro Automotive Product Lite | Automotive | $218.08 | 4 | ★ 2.6 | 685 | SKU-000253 |
| 254 | Professional Home & Garden Product Pro | Home & Garden | $674.83 | 69 | ★ 3.3 | 97 | SKU-000254 |
| 255 | Standard Electronics Product Lite | Electronics | $450.81 | 206 | ★ 1.9 | 765 | SKU-000255 |
| 256 | Elite Toys & Games Product 2024 | Toys & Games | $411.15 | 408 | ★ 4.7 | 673 | SKU-000256 |
| 257 | Economy Toys & Games Product Advanced | Toys & Games | $451.59 | 141 | ★ 2.0 | 515 | SKU-000257 |
| 258 | Professional Food & Grocery Product Pro | Food & Grocery | $69.33 | 290 | ★ 2.8 | 745 | SKU-000258 |
| 259 | Super Electronics Product Mini | Electronics | $371.47 | 231 | ★ 5.0 | 221 | SKU-000259 |
| 260 | Ultra Office Supplies Product Advanced | Office Supplies | $557.28 | 150 | ★ 2.5 | 50 | SKU-000260 |
| 261 | Professional Books Product Special Edition | Books | $354.94 | 314 | ★ 2.1 | 54 | SKU-000261 |
| 262 | Elite Food & Grocery Product Pro | Food & Grocery | $451.76 | 409 | ★ 3.0 | 757 | SKU-000262 |
| 263 | Elite Books Product Pro | Books | $443.88 | 1 | ★ 4.6 | 904 | SKU-000263 |
| 264 | Mega Health & Beauty Product Classic | Health & Beauty | $134.76 | 386 | ★ 1.4 | 406 | SKU-000264 |
| 265 | Mega Electronics Product Classic | Electronics | $193.78 | 458 | ★ 3.5 | 923 | SKU-000265 |
| 266 | Deluxe Food & Grocery Product 2024 | Food & Grocery | $223.10 | 0 | ★ 1.3 | 848 | SKU-000266 |
| 267 | Economy Clothing Product 2024 | Clothing | $359.16 | 266 | ★ 4.1 | 221 | SKU-000267 |
| 268 | Super Books Product Pro | Books | $779.05 | 259 | ★ 3.2 | 261 | SKU-000268 |
| 271 | Elite Toys & Games Product Max | Toys & Games | $377.34 | 470 | ★ 2.1 | 602 | SKU-000271 |
| 272 | Ultra Electronics Product Max | Electronics | $894.91 | 368 | ★ 4.6 | 380 | SKU-000272 |
| 273 | Pro Office Supplies Product Plus | Office Supplies | $857.30 | 425 | ★ 2.7 | 83 | SKU-000273 |
| 274 | Economy Toys & Games Product Plus | Toys & Games | $900.40 | 259 | ★ 2.3 | 454 | SKU-000274 |
| 275 | Elite Office Supplies Product Lite | Office Supplies | $421.59 | 45 | ★ 2.3 | 821 | SKU-000275 |
| 276 | Ultra Food & Grocery Product Special Edition | Food & Grocery | $14.61 | 101 | ★ 2.3 | 379 | SKU-000276 |
// 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
};
}