Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 8235 | Bulk Product eb1c41cb | Electronics | $770.37 | 342 | ★ 1.0 | 1 | BULK-dda55728 |
| 8236 | Bulk Product 30b4e936 | Books | $910.34 | 469 | ★ 0.2 | 32 | BULK-2dbcec04 |
| 8237 | Bulk Product f643c96b | Clothing | $715.10 | 68 | ★ 2.0 | 26 | BULK-e7daa652 |
| 8238 | Bulk Product b599b890 | Toys & Games | $955.38 | 193 | ★ 3.5 | 54 | BULK-ff49da9f |
| 8239 | Bulk Product a7bceb48 | Toys & Games | $883.43 | 87 | ★ 3.6 | 80 | BULK-3aa42b7e |
| 8240 | Bulk Product f9da8d84 | Toys & Games | $824.24 | 139 | ★ 0.5 | 87 | BULK-a602e50e |
| 8241 | Bulk Product 350f47f2 | Sports & Outdoors | $674.75 | 249 | ★ 1.5 | 76 | BULK-e5db8e81 |
| 8242 | Bulk Product b10d9ca6 | Toys & Games | $547.58 | 477 | ★ 3.9 | 21 | BULK-ed6e7beb |
| 8243 | Bulk Product 28c9e5a4 | Clothing | $892.25 | 80 | ★ 3.1 | 38 | BULK-f83b71f0 |
| 8244 | Bulk Product 7f1cf406 | Toys & Games | $503.76 | 276 | ★ 1.9 | 37 | BULK-703f9f7c |
| 8245 | Bulk Product 25da87ab | Electronics | $250.35 | 300 | ★ 2.7 | 84 | BULK-3f5b8c75 |
| 8246 | Bulk Product 6bf67508 | Sports & Outdoors | $367.34 | 430 | ★ 2.4 | 26 | BULK-aa068712 |
| 8247 | Bulk Product 784909f0 | Electronics | $406.54 | 77 | ★ 2.5 | 83 | BULK-49b9f101 |
| 8248 | Bulk Product 4f07fa6e | Clothing | $350.98 | 1 | ★ 0.2 | 69 | BULK-076c152d |
| 8249 | Bulk Product 4895073f | Home & Garden | $996.82 | 381 | ★ 1.8 | 90 | BULK-48b37343 |
| 8250 | Bulk Product 0762a2c8 | Sports & Outdoors | $127.26 | 430 | ★ 1.1 | 36 | BULK-2a985d65 |
| 8251 | Bulk Product a0c1e1ed | Books | $786.54 | 130 | ★ 0.3 | 29 | BULK-bb3aa03f |
| 8252 | Bulk Product 0db3960e | Books | $998.75 | 176 | ★ 1.6 | 46 | BULK-eb646884 |
| 8253 | Bulk Product 980a2fea | Clothing | $659.05 | 462 | ★ 1.8 | 29 | BULK-740b36d4 |
| 8254 | Bulk Product e9123e02 | Home & Garden | $305.81 | 156 | ★ 1.2 | 99 | BULK-7cf59096 |
| 8255 | Bulk Product 37b01ccc | Home & Garden | $353.62 | 91 | ★ 1.3 | 76 | BULK-f7d59011 |
| 8256 | Bulk Product 6e389212 | Electronics | $11.50 | 385 | ★ 4.3 | 97 | BULK-22e96380 |
| 8257 | Bulk Product b0803dd2 | Electronics | $950.48 | 222 | ★ 0.3 | 74 | BULK-22b5be05 |
| 8258 | Bulk Product 5000479a | Sports & Outdoors | $938.41 | 472 | ★ 1.2 | 67 | BULK-080b31ae |
| 8259 | Bulk Product cffd154b | Clothing | $568.14 | 446 | ★ 2.7 | 99 | BULK-6c92de4b |
// 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
};
}