Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 24660 | Bulk Product a9d9aa0f | Electronics | $161.24 | 227 | ★ 4.3 | 32 | BULK-b68f6a9f |
| 24661 | Bulk Product 2f97a38a | Toys & Games | $58.75 | 390 | ★ 3.7 | 94 | BULK-77da57a0 |
| 24662 | Bulk Product dfab26fb | Home & Garden | $995.37 | 64 | ★ 3.5 | 68 | BULK-0cf9cac4 |
| 24663 | Bulk Product 03956e3e | Books | $724.42 | 34 | ★ 4.7 | 35 | BULK-9f49b43a |
| 24664 | Bulk Product 81ea9a51 | Sports & Outdoors | $256.73 | 25 | ★ 2.2 | 96 | BULK-e17af7a2 |
| 24665 | Bulk Product 6b4641e5 | Toys & Games | $885.44 | 101 | ★ 0.3 | 62 | BULK-3edbf3e5 |
| 24666 | Bulk Product f9f13827 | Clothing | $350.91 | 153 | ★ 3.5 | 82 | BULK-1070e341 |
| 24667 | Bulk Product 01c2f6eb | Books | $771.86 | 451 | ★ 0.9 | 22 | BULK-03e2cc2e |
| 24668 | Bulk Product 4b4705ec | Clothing | $744.62 | 427 | ★ 0.7 | 66 | BULK-3b2d4213 |
| 24669 | Bulk Product d36ca176 | Books | $244.55 | 118 | ★ 2.5 | 84 | BULK-77a527f0 |
| 24670 | Bulk Product bb772d48 | Clothing | $204.74 | 220 | ★ 1.3 | 75 | BULK-d59d746d |
| 24671 | Bulk Product 8c71d3b5 | Clothing | $495.04 | 391 | ★ 2.0 | 95 | BULK-13901002 |
| 24672 | Bulk Product 1d8fff89 | Toys & Games | $951.70 | 134 | ★ 3.5 | 21 | BULK-413032a0 |
| 24673 | Bulk Product 6637ad72 | Sports & Outdoors | $800.04 | 89 | ★ 1.3 | 12 | BULK-ea3f1b78 |
| 24674 | Bulk Product c6a97bb1 | Home & Garden | $571.81 | 230 | ★ 1.1 | 37 | BULK-45afbca5 |
| 24675 | Bulk Product de4978ac | Clothing | $656.08 | 380 | ★ 3.3 | 6 | BULK-91287d6a |
| 24676 | Bulk Product 272e28c4 | Books | $698.97 | 344 | ★ 4.8 | 64 | BULK-7e59ce81 |
| 24677 | Bulk Product 58841fcf | Clothing | $574.67 | 119 | ★ 3.2 | 86 | BULK-80b24003 |
| 24678 | Bulk Product 193fe0d1 | Clothing | $782.07 | 306 | ★ 0.7 | 96 | BULK-f643883e |
| 24679 | Bulk Product 3f2ade7d | Sports & Outdoors | $952.10 | 139 | ★ 0.5 | 17 | BULK-5c5b5c05 |
| 24680 | Bulk Product a9bb5800 | Books | $653.78 | 68 | ★ 1.5 | 4 | BULK-9a27277f |
| 24681 | Bulk Product 22af4f53 | Toys & Games | $328.67 | 287 | ★ 4.7 | 35 | BULK-d8ec0b76 |
| 24682 | Bulk Product 50527222 | Sports & Outdoors | $553.98 | 94 | ★ 2.8 | 5 | BULK-9bb7c284 |
| 24683 | Bulk Product 9aae81cb | Home & Garden | $666.29 | 396 | ★ 3.2 | 40 | BULK-f15156dd |
| 24684 | Bulk Product ecd2ca58 | Books | $188.95 | 307 | ★ 0.2 | 7 | BULK-31628e2d |
// 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
};
}