Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 21160 | Bulk Product f31937e3 | Books | $721.38 | 239 | ★ 3.9 | 20 | BULK-1df5c85d |
| 21161 | Bulk Product 6c8d4f45 | Sports & Outdoors | $133.37 | 396 | ★ 0.3 | 45 | BULK-b128b065 |
| 21162 | Bulk Product 89b08153 | Electronics | $752.61 | 488 | ★ 2.6 | 37 | BULK-e0b8271e |
| 21163 | Bulk Product 5806fea3 | Home & Garden | $40.86 | 91 | ★ 2.4 | 41 | BULK-eb101144 |
| 21164 | Bulk Product 0e1d8ddb | Clothing | $655.26 | 251 | ★ 2.6 | 14 | BULK-b85b4ff6 |
| 21165 | Bulk Product 3c96ea2b | Clothing | $457.16 | 290 | ★ 4.8 | 10 | BULK-60e818da |
| 21166 | Bulk Product 95d980fe | Electronics | $41.92 | 111 | ★ 2.2 | 86 | BULK-667ccb40 |
| 21167 | Bulk Product d2a2950e | Electronics | $24.70 | 128 | ★ 2.2 | 96 | BULK-500ad7db |
| 21168 | Bulk Product cef435b2 | Toys & Games | $110.89 | 187 | ★ 4.4 | 99 | BULK-d228d40c |
| 21169 | Bulk Product 89b4ed5e | Electronics | $85.60 | 116 | ★ 3.0 | 74 | BULK-5acc1b61 |
| 21170 | Bulk Product faa7479b | Clothing | $12.65 | 279 | ★ 2.1 | 51 | BULK-a5930b0b |
| 21171 | Bulk Product e5138bee | Clothing | $336.98 | 103 | ★ 1.3 | 22 | BULK-40feb712 |
| 21172 | Bulk Product d888fb69 | Clothing | $315.45 | 44 | ★ 2.0 | 59 | BULK-461dde96 |
| 21173 | Bulk Product f30d1164 | Books | $565.47 | 255 | ★ 3.5 | 62 | BULK-629bc434 |
| 21174 | Bulk Product a91fd9c5 | Sports & Outdoors | $954.03 | 326 | ★ 3.5 | 97 | BULK-eef0c8b3 |
| 21175 | Bulk Product 01b5dd34 | Home & Garden | $554.41 | 261 | ★ 4.5 | 50 | BULK-6d52a72d |
| 21176 | Bulk Product 7e3c366e | Electronics | $498.01 | 424 | ★ 2.8 | 7 | BULK-7b0123c3 |
| 21177 | Bulk Product 47ac3f9f | Books | $600.76 | 152 | ★ 2.5 | 29 | BULK-1ac2c85c |
| 21178 | Bulk Product 226a90e8 | Toys & Games | $539.09 | 365 | ★ 0.4 | 88 | BULK-48e2c88e |
| 21179 | Bulk Product f4c6d3d2 | Books | $630.20 | 323 | ★ 0.4 | 72 | BULK-90eb2fb8 |
| 21180 | Bulk Product f6ad4a70 | Toys & Games | $634.46 | 60 | ★ 2.6 | 72 | BULK-d144ab9b |
| 21181 | Bulk Product dcd17a26 | Toys & Games | $349.33 | 403 | ★ 2.4 | 38 | BULK-d1c3fded |
| 21182 | Bulk Product 5ba78074 | Books | $113.78 | 184 | ★ 1.6 | 94 | BULK-c71e7bfd |
| 21183 | Bulk Product d4b4f90c | Electronics | $749.94 | 325 | ★ 3.1 | 87 | BULK-ed9578a3 |
| 21184 | Bulk Product e66465ec | Electronics | $827.61 | 118 | ★ 3.9 | 31 | BULK-4f084fad |
// 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
};
}