Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 16235 | Bulk Product 4458c885 | Home & Garden | $129.37 | 213 | ★ 4.7 | 24 | BULK-848b4397 |
| 16236 | Bulk Product da615c70 | Clothing | $239.64 | 135 | ★ 2.2 | 54 | BULK-58c93240 |
| 16237 | Bulk Product f4f02e6d | Toys & Games | $257.25 | 247 | ★ 0.8 | 43 | BULK-8304da68 |
| 16238 | Bulk Product b35f3b9a | Books | $121.22 | 330 | ★ 4.9 | 89 | BULK-50f3e11d |
| 16239 | Bulk Product 4fa88079 | Books | $565.81 | 279 | ★ 4.5 | 85 | BULK-e99d4bfe |
| 16240 | Bulk Product f4128d54 | Home & Garden | $938.74 | 65 | ★ 1.2 | 8 | BULK-044089fd |
| 16241 | Bulk Product 6a661ec7 | Home & Garden | $915.72 | 124 | ★ 2.5 | 32 | BULK-435e3dda |
| 16242 | Bulk Product f5f188ca | Books | $250.52 | 231 | ★ 1.3 | 49 | BULK-bee17d2d |
| 16243 | Bulk Product bc16236b | Books | $878.22 | 138 | ★ 1.5 | 3 | BULK-6befc08a |
| 16244 | Bulk Product 151dcf60 | Books | $915.00 | 80 | ★ 4.1 | 84 | BULK-cfb57496 |
| 16245 | Bulk Product 53897560 | Toys & Games | $448.04 | 492 | ★ 4.0 | 34 | BULK-a26f8cb2 |
| 16246 | Bulk Product e8ebb383 | Toys & Games | $857.63 | 155 | ★ 1.3 | 99 | BULK-7be0a766 |
| 16247 | Bulk Product 4c931327 | Electronics | $572.31 | 205 | ★ 0.5 | 27 | BULK-c037dd85 |
| 16248 | Bulk Product cf358c7f | Toys & Games | $335.00 | 30 | ★ 0.2 | 99 | BULK-2fbefe51 |
| 16249 | Bulk Product 81533f61 | Clothing | $38.80 | 215 | ★ 4.8 | 87 | BULK-cd059f5a |
| 16250 | Bulk Product b0461440 | Toys & Games | $689.95 | 52 | ★ 3.1 | 54 | BULK-73412965 |
| 16251 | Bulk Product c46a66b6 | Home & Garden | $264.71 | 194 | ★ 0.8 | 11 | BULK-14e8bee3 |
| 16252 | Bulk Product 1ab18e0f | Home & Garden | $1,008.21 | 202 | ★ 1.0 | 15 | BULK-d1ca20f6 |
| 16253 | Bulk Product 81c75bec | Clothing | $100.47 | 198 | ★ 3.4 | 27 | BULK-2e0f32a3 |
| 16254 | Bulk Product b16c896d | Clothing | $84.44 | 290 | ★ 2.5 | 76 | BULK-251b9329 |
| 16255 | Bulk Product 48e68c76 | Books | $210.89 | 453 | ★ 0.9 | 22 | BULK-20e57f74 |
| 16256 | Bulk Product 021f3adf | Books | $711.23 | 99 | ★ 2.6 | 36 | BULK-e6bac9d6 |
| 16257 | Bulk Product d6f6c26c | Sports & Outdoors | $640.86 | 257 | ★ 0.8 | 57 | BULK-4202e6c4 |
| 16258 | Bulk Product a5da0763 | Books | $170.88 | 137 | ★ 2.8 | 87 | BULK-74301915 |
| 16259 | Bulk Product ed6d4370 | Electronics | $992.71 | 204 | ★ 3.7 | 58 | BULK-8fa41a33 |
// 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
};
}