Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 5485 | Bulk Product 3bff3548 | Electronics | $175.62 | 362 | ★ 0.1 | 44 | BULK-5057f7b5 |
| 5486 | Bulk Product e2fde0d0 | Home & Garden | $318.63 | 470 | ★ 2.8 | 21 | BULK-8821b391 |
| 5487 | Bulk Product 69573129 | Books | $638.03 | 96 | ★ 2.0 | 92 | BULK-2bf481a5 |
| 5488 | Bulk Product b0506d5e | Toys & Games | $599.08 | 305 | ★ 2.0 | 36 | BULK-a360a9f1 |
| 5489 | Bulk Product 07acdcf0 | Clothing | $397.76 | 183 | ★ 4.8 | 89 | BULK-5aed8ad8 |
| 5490 | Bulk Product 22afd0a8 | Sports & Outdoors | $323.75 | 376 | ★ 3.6 | 90 | BULK-e708f1d8 |
| 5491 | Bulk Product fc7fce30 | Clothing | $653.64 | 275 | ★ 4.2 | 8 | BULK-73a77b79 |
| 5492 | Bulk Product a034b3bd | Home & Garden | $478.91 | 333 | ★ 1.7 | 17 | BULK-091290e0 |
| 5493 | Bulk Product b3fd53c6 | Home & Garden | $129.03 | 378 | ★ 3.4 | 57 | BULK-4e468cff |
| 5494 | Bulk Product 6f2a436d | Toys & Games | $475.59 | 373 | ★ 3.9 | 36 | BULK-de2da839 |
| 5495 | Bulk Product 21a983f6 | Toys & Games | $52.29 | 435 | ★ 5.0 | 12 | BULK-fa629afd |
| 5496 | Bulk Product f8d0ca5e | Electronics | $995.95 | 86 | ★ 1.1 | 37 | BULK-c62a10a6 |
| 5497 | Bulk Product 6f358877 | Home & Garden | $187.93 | 18 | ★ 1.3 | 54 | BULK-0468a5fe |
| 5498 | Bulk Product c532239d | Books | $589.04 | 119 | ★ 0.5 | 97 | BULK-8eed4b9c |
| 5499 | Bulk Product 16b3ef6c | Electronics | $751.63 | 83 | ★ 3.1 | 90 | BULK-07190e3f |
| 5500 | Bulk Product 9121418c | Sports & Outdoors | $555.81 | 217 | ★ 4.7 | 22 | BULK-06d30c3c |
| 5501 | Bulk Product 4a276baf | Clothing | $920.90 | 256 | ★ 1.1 | 24 | BULK-012a3696 |
| 5502 | Bulk Product be590e6e | Books | $670.20 | 170 | ★ 2.9 | 13 | BULK-c83a2454 |
| 5503 | Bulk Product 2857115f | Electronics | $998.79 | 373 | ★ 4.3 | 57 | BULK-cf003851 |
| 5504 | Bulk Product 36b68db7 | Toys & Games | $755.65 | 342 | ★ 0.0 | 39 | BULK-9fdb8baf |
| 5505 | Bulk Product 72257932 | Home & Garden | $69.95 | 293 | ★ 3.1 | 30 | BULK-da99a3c2 |
| 5506 | Bulk Product 54c44fef | Sports & Outdoors | $192.73 | 171 | ★ 0.4 | 39 | BULK-b45a0253 |
| 5507 | Bulk Product 07be6f34 | Electronics | $861.98 | 221 | ★ 2.5 | 84 | BULK-fdd89f69 |
| 5508 | Bulk Product 91bbffa2 | Sports & Outdoors | $1,009.41 | 312 | ★ 4.8 | 33 | BULK-6aa5b56d |
| 5509 | Bulk Product 500ad8a0 | Toys & Games | $473.96 | 78 | ★ 0.8 | 41 | BULK-2eddff30 |
// 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
};
}