Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 20985 | Bulk Product aa29d732 | Home & Garden | $881.22 | 248 | ★ 1.2 | 52 | BULK-43f8faa2 |
| 20986 | Bulk Product eff5bba7 | Books | $454.63 | 2 | ★ 2.8 | 16 | BULK-15c23ddc |
| 20987 | Bulk Product db2f1075 | Books | $921.82 | 41 | ★ 2.9 | 27 | BULK-8e7d5df2 |
| 20988 | Bulk Product f0aa8d6e | Books | $685.01 | 201 | ★ 5.0 | 7 | BULK-dc27a138 |
| 20989 | Bulk Product 0b87fa4a | Toys & Games | $902.32 | 399 | ★ 1.0 | 64 | BULK-e4149553 |
| 20990 | Bulk Product d03a87d7 | Toys & Games | $1,006.58 | 73 | ★ 4.7 | 82 | BULK-d61c2430 |
| 20991 | Bulk Product 312c5b0f | Clothing | $882.31 | 220 | ★ 1.3 | 55 | BULK-3ac51599 |
| 20992 | Bulk Product 33a8fa4a | Electronics | $709.45 | 363 | ★ 1.2 | 15 | BULK-62c9d929 |
| 20993 | Bulk Product 6924b0cc | Clothing | $363.37 | 163 | ★ 2.1 | 14 | BULK-0e901087 |
| 20994 | Bulk Product 45428dc7 | Clothing | $353.56 | 293 | ★ 3.3 | 42 | BULK-2d3d65e4 |
| 20995 | Bulk Product 7224fff5 | Toys & Games | $236.84 | 416 | ★ 3.1 | 39 | BULK-548015e2 |
| 20996 | Bulk Product 8f31a95a | Home & Garden | $965.38 | 184 | ★ 3.0 | 15 | BULK-fdc4d5bf |
| 20997 | Bulk Product 5a9c77e0 | Clothing | $248.64 | 443 | ★ 2.9 | 10 | BULK-670b553b |
| 20998 | Bulk Product 4f91e026 | Toys & Games | $934.45 | 344 | ★ 5.0 | 0 | BULK-88728727 |
| 20999 | Bulk Product 02ad7fb8 | Sports & Outdoors | $12.43 | 145 | ★ 3.6 | 86 | BULK-646934e6 |
| 21000 | Bulk Product d93cdfbe | Toys & Games | $399.20 | 442 | ★ 0.0 | 11 | BULK-7001f010 |
| 21001 | Bulk Product 2ae3c84b | Clothing | $337.43 | 329 | ★ 1.0 | 27 | BULK-09ae050a |
| 21002 | Bulk Product 06ced327 | Sports & Outdoors | $842.00 | 419 | ★ 1.7 | 0 | BULK-9d6eaa3a |
| 21003 | Bulk Product 098d9d3f | Clothing | $144.43 | 280 | ★ 1.6 | 56 | BULK-54dfd71d |
| 21004 | Bulk Product e949edd0 | Sports & Outdoors | $96.49 | 404 | ★ 2.9 | 49 | BULK-b82bc140 |
| 21005 | Bulk Product 0feb65f3 | Clothing | $190.91 | 485 | ★ 1.2 | 47 | BULK-da66b8a7 |
| 21006 | Bulk Product 4ad6b309 | Toys & Games | $1,006.83 | 130 | ★ 1.0 | 96 | BULK-9fd0e89b |
| 21007 | Bulk Product 0fcd4988 | Books | $424.93 | 167 | ★ 0.0 | 68 | BULK-fe758b01 |
| 21008 | Bulk Product 744c1903 | Home & Garden | $234.88 | 350 | ★ 0.1 | 99 | BULK-7b9bbf5d |
| 21009 | Bulk Product a4f74e05 | Clothing | $775.15 | 70 | ★ 1.1 | 91 | BULK-756eef0b |
// 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
};
}