Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 21260 | Bulk Product 450ef634 | Clothing | $797.90 | 355 | ★ 2.7 | 85 | BULK-a92f98e3 |
| 21261 | Bulk Product bcf3413e | Sports & Outdoors | $218.43 | 104 | ★ 2.0 | 14 | BULK-dc17d021 |
| 21262 | Bulk Product 7d1a6f44 | Sports & Outdoors | $113.55 | 113 | ★ 3.6 | 69 | BULK-4a32975c |
| 21263 | Bulk Product 00fdd445 | Sports & Outdoors | $755.16 | 171 | ★ 0.0 | 89 | BULK-bee6beed |
| 21264 | Bulk Product c03c2e7d | Toys & Games | $146.82 | 268 | ★ 3.3 | 47 | BULK-f512745a |
| 21265 | Bulk Product c54f5961 | Electronics | $276.49 | 404 | ★ 4.1 | 77 | BULK-df0d1ac8 |
| 21266 | Bulk Product 4a4126c4 | Electronics | $511.32 | 173 | ★ 1.7 | 69 | BULK-78f1b68b |
| 21267 | Bulk Product 28278251 | Books | $352.38 | 365 | ★ 1.0 | 68 | BULK-e9d56f2f |
| 21268 | Bulk Product 9ea7505f | Toys & Games | $893.20 | 260 | ★ 1.6 | 56 | BULK-cd6396be |
| 21269 | Bulk Product c76520a9 | Books | $565.76 | 150 | ★ 1.1 | 30 | BULK-a899cf9a |
| 21270 | Bulk Product 3406185d | Sports & Outdoors | $916.05 | 303 | ★ 0.2 | 36 | BULK-50df97f3 |
| 21271 | Bulk Product 010826e7 | Sports & Outdoors | $708.03 | 458 | ★ 3.4 | 98 | BULK-ab2c9477 |
| 21272 | Bulk Product 9120dc31 | Electronics | $699.41 | 406 | ★ 1.5 | 76 | BULK-4b2a3a4e |
| 21273 | Bulk Product 1df1f326 | Electronics | $586.24 | 425 | ★ 2.3 | 91 | BULK-46b8a5df |
| 21274 | Bulk Product a4adf207 | Clothing | $114.71 | 135 | ★ 3.7 | 64 | BULK-016135f6 |
| 21275 | Bulk Product 7d6b2321 | Books | $678.29 | 11 | ★ 1.1 | 53 | BULK-201b8487 |
| 21276 | Bulk Product 4da535c8 | Electronics | $718.99 | 125 | ★ 2.5 | 43 | BULK-cb9a5597 |
| 21277 | Bulk Product 7d51d53d | Home & Garden | $379.78 | 64 | ★ 2.2 | 87 | BULK-92c27aa0 |
| 21278 | Bulk Product 5e26af84 | Electronics | $878.37 | 274 | ★ 3.0 | 36 | BULK-7f7a0bd1 |
| 21279 | Bulk Product 7b63b143 | Electronics | $1,003.73 | 232 | ★ 3.8 | 24 | BULK-e5b92be9 |
| 21280 | Bulk Product 4f8c6379 | Home & Garden | $951.23 | 15 | ★ 3.3 | 10 | BULK-a486d1cc |
| 21281 | Bulk Product e9a0474c | Sports & Outdoors | $162.38 | 494 | ★ 3.9 | 22 | BULK-db3c51b1 |
| 21282 | Bulk Product 087f2299 | Clothing | $995.55 | 107 | ★ 4.0 | 67 | BULK-a144e0ce |
| 21283 | Bulk Product 591dc8ec | Books | $853.34 | 437 | ★ 2.9 | 28 | BULK-bdabe580 |
| 21284 | Bulk Product d4da2dea | Books | $170.50 | 451 | ★ 4.7 | 56 | BULK-14fa7ff1 |
// 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
};
}