Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 22185 | Bulk Product 10a33665 | Sports & Outdoors | $545.22 | 267 | ★ 3.8 | 28 | BULK-2cdbc416 |
| 22186 | Bulk Product 04a666e6 | Sports & Outdoors | $357.14 | 58 | ★ 3.8 | 40 | BULK-9f791a5f |
| 22187 | Bulk Product 6a196b38 | Clothing | $861.14 | 360 | ★ 4.5 | 91 | BULK-43f499e8 |
| 22188 | Bulk Product d3657672 | Toys & Games | $758.17 | 415 | ★ 2.4 | 89 | BULK-6b1016a1 |
| 22189 | Bulk Product b96129aa | Home & Garden | $719.16 | 406 | ★ 2.6 | 81 | BULK-e1e93283 |
| 22190 | Bulk Product c6c37d59 | Home & Garden | $935.15 | 348 | ★ 4.1 | 4 | BULK-2e45fe9d |
| 22191 | Bulk Product b5a11a92 | Toys & Games | $949.91 | 163 | ★ 0.7 | 46 | BULK-2d450bd9 |
| 22192 | Bulk Product e2eb10fd | Toys & Games | $597.34 | 170 | ★ 3.5 | 6 | BULK-f0ecbc66 |
| 22193 | Bulk Product 85b7c446 | Toys & Games | $170.40 | 216 | ★ 2.8 | 40 | BULK-7d789aa1 |
| 22194 | Bulk Product ee6deaca | Home & Garden | $901.82 | 310 | ★ 3.8 | 6 | BULK-f4c39d2a |
| 22195 | Bulk Product 0878eb9e | Books | $241.82 | 275 | ★ 0.7 | 13 | BULK-cf05d275 |
| 22196 | Bulk Product 8b1a3c82 | Clothing | $896.18 | 420 | ★ 3.0 | 85 | BULK-2207b51b |
| 22197 | Bulk Product 113e7f44 | Books | $143.12 | 338 | ★ 0.6 | 3 | BULK-309fa256 |
| 22198 | Bulk Product 244ac8ee | Books | $197.33 | 276 | ★ 4.3 | 63 | BULK-695d053d |
| 22199 | Bulk Product be3a0308 | Toys & Games | $986.67 | 66 | ★ 4.3 | 73 | BULK-9967b1b0 |
| 22200 | Bulk Product e78431a3 | Home & Garden | $802.01 | 50 | ★ 1.3 | 14 | BULK-169e02e0 |
| 22201 | Bulk Product 4a408900 | Electronics | $355.25 | 405 | ★ 4.7 | 23 | BULK-d9eb143f |
| 22202 | Bulk Product b410899f | Home & Garden | $272.22 | 310 | ★ 5.0 | 11 | BULK-bc4e5d1b |
| 22203 | Bulk Product 121e039e | Electronics | $616.86 | 308 | ★ 3.0 | 95 | BULK-cb409684 |
| 22204 | Bulk Product 8a1e576c | Sports & Outdoors | $174.07 | 179 | ★ 2.4 | 95 | BULK-8493b610 |
| 22205 | Bulk Product 2ed54306 | Electronics | $193.11 | 124 | ★ 0.6 | 33 | BULK-3103fa8a |
| 22206 | Bulk Product 91830625 | Electronics | $71.86 | 228 | ★ 3.9 | 76 | BULK-1cd77f96 |
| 22207 | Bulk Product 5ba07a31 | Clothing | $298.41 | 71 | ★ 4.5 | 69 | BULK-fc6b4fed |
| 22208 | Bulk Product 61846eed | Home & Garden | $104.05 | 473 | ★ 2.8 | 98 | BULK-d06c460f |
| 22209 | Bulk Product e6f79cec | Home & Garden | $64.13 | 397 | ★ 0.5 | 74 | BULK-d1f44a13 |
// 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
};
}