Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 20185 | Bulk Product 57635112 | Clothing | $327.56 | 238 | ★ 2.7 | 25 | BULK-1e67a1ea |
| 20186 | Bulk Product ad4ebcfa | Sports & Outdoors | $825.38 | 366 | ★ 1.1 | 44 | BULK-f09e88df |
| 20187 | Bulk Product d95ceb75 | Books | $533.26 | 89 | ★ 2.8 | 44 | BULK-c3bd88c8 |
| 20188 | Bulk Product bfe71140 | Clothing | $271.43 | 62 | ★ 3.1 | 58 | BULK-bd72c71e |
| 20189 | Bulk Product 7e69e2d5 | Books | $282.74 | 282 | ★ 1.9 | 92 | BULK-52bff021 |
| 20190 | Bulk Product 1eb02e7d | Toys & Games | $490.16 | 58 | ★ 3.1 | 8 | BULK-8235c6db |
| 20191 | Bulk Product ed9580c5 | Clothing | $757.58 | 56 | ★ 3.3 | 75 | BULK-fde8c10a |
| 20192 | Bulk Product 37e23765 | Clothing | $580.58 | 394 | ★ 0.1 | 54 | BULK-f87d3440 |
| 20193 | Bulk Product fc28ad40 | Home & Garden | $510.24 | 485 | ★ 2.1 | 17 | BULK-bd18e13d |
| 20194 | Bulk Product b85f51e0 | Electronics | $236.70 | 484 | ★ 3.9 | 77 | BULK-d811285f |
| 20195 | Bulk Product 75a28055 | Clothing | $677.46 | 174 | ★ 4.4 | 13 | BULK-53193f03 |
| 20196 | Bulk Product 296b50cf | Electronics | $800.69 | 277 | ★ 3.8 | 39 | BULK-7a284c29 |
| 20197 | Bulk Product 12822724 | Home & Garden | $270.39 | 304 | ★ 1.6 | 99 | BULK-649e7404 |
| 20198 | Bulk Product 20cf5b68 | Toys & Games | $979.89 | 437 | ★ 3.2 | 15 | BULK-7df0dd93 |
| 20199 | Bulk Product 918613e8 | Books | $987.60 | 198 | ★ 3.8 | 13 | BULK-fb14854a |
| 20200 | Bulk Product d1df325f | Clothing | $725.98 | 405 | ★ 3.9 | 3 | BULK-0fd6db1f |
| 20201 | Bulk Product e1545324 | Electronics | $912.64 | 375 | ★ 3.1 | 11 | BULK-7b39a76b |
| 20202 | Bulk Product bd8d00f2 | Clothing | $22.22 | 376 | ★ 0.6 | 94 | BULK-01aaf03d |
| 20203 | Bulk Product a0de0a56 | Sports & Outdoors | $707.15 | 4 | ★ 2.2 | 8 | BULK-03edd4d0 |
| 20204 | Bulk Product 12cbaca2 | Toys & Games | $197.12 | 115 | ★ 4.3 | 18 | BULK-c004e06f |
| 20205 | Bulk Product b0d89912 | Electronics | $93.79 | 436 | ★ 2.9 | 55 | BULK-0a233561 |
| 20206 | Bulk Product d095a95c | Toys & Games | $137.68 | 381 | ★ 2.4 | 88 | BULK-b0e237b3 |
| 20207 | Bulk Product bba509b0 | Books | $690.61 | 213 | ★ 1.5 | 5 | BULK-4b040bfc |
| 20208 | Bulk Product 30a370b8 | Home & Garden | $204.50 | 261 | ★ 1.6 | 57 | BULK-17747414 |
| 20209 | Bulk Product 17dfa336 | Toys & Games | $309.26 | 105 | ★ 4.1 | 63 | BULK-961a6439 |
// 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
};
}