Efficient pagination using OFFSET-FETCH on SQL Server.
| ID | Name | Category | Price | Stock | Rating | Reviews | SKU |
|---|---|---|---|---|---|---|---|
| 20910 | Bulk Product 3ff34373 | Toys & Games | $664.84 | 296 | ★ 0.0 | 99 | BULK-fbbb4297 |
| 20911 | Bulk Product bd7acb99 | Books | $637.18 | 425 | ★ 3.4 | 5 | BULK-b44ad849 |
| 20912 | Bulk Product ade92815 | Clothing | $966.65 | 184 | ★ 4.5 | 58 | BULK-fb534ec6 |
| 20913 | Bulk Product 004dfb3e | Toys & Games | $569.95 | 359 | ★ 0.4 | 10 | BULK-395d7bb5 |
| 20914 | Bulk Product f6662e18 | Clothing | $834.26 | 254 | ★ 0.5 | 32 | BULK-2fb50c73 |
| 20915 | Bulk Product e5fd4d39 | Sports & Outdoors | $472.35 | 260 | ★ 3.0 | 50 | BULK-e693b216 |
| 20916 | Bulk Product 9d491943 | Electronics | $370.46 | 96 | ★ 0.6 | 54 | BULK-c4849b57 |
| 20917 | Bulk Product cf590c2d | Toys & Games | $802.29 | 132 | ★ 1.3 | 21 | BULK-c62201d5 |
| 20918 | Bulk Product 3d5b212b | Clothing | $469.70 | 354 | ★ 4.8 | 77 | BULK-93dbcfdc |
| 20919 | Bulk Product 94325bda | Clothing | $30.81 | 463 | ★ 4.4 | 11 | BULK-531a6b4f |
| 20920 | Bulk Product 085222d8 | Toys & Games | $216.71 | 63 | ★ 1.7 | 76 | BULK-76090a90 |
| 20921 | Bulk Product 31259d11 | Home & Garden | $318.50 | 99 | ★ 2.7 | 58 | BULK-688c2b4a |
| 20922 | Bulk Product 4ce405f5 | Electronics | $413.68 | 396 | ★ 3.8 | 24 | BULK-9a7562d4 |
| 20923 | Bulk Product 32ed3d68 | Home & Garden | $806.32 | 13 | ★ 3.6 | 30 | BULK-2893fba8 |
| 20924 | Bulk Product 6eb9fb2c | Sports & Outdoors | $759.71 | 93 | ★ 2.5 | 96 | BULK-80b3adec |
| 20925 | Bulk Product 0f02d069 | Sports & Outdoors | $798.69 | 123 | ★ 2.6 | 58 | BULK-0d6579a8 |
| 20926 | Bulk Product ddae41f9 | Sports & Outdoors | $926.75 | 97 | ★ 2.4 | 65 | BULK-8be78516 |
| 20927 | Bulk Product a9252fdd | Sports & Outdoors | $525.55 | 43 | ★ 4.2 | 7 | BULK-41a93a45 |
| 20928 | Bulk Product 9d6c7034 | Home & Garden | $86.59 | 111 | ★ 3.9 | 57 | BULK-1069ef75 |
| 20929 | Bulk Product 5181691d | Electronics | $135.59 | 29 | ★ 0.0 | 62 | BULK-f8741336 |
| 20930 | Bulk Product 5b73d747 | Electronics | $746.71 | 155 | ★ 1.0 | 2 | BULK-a5a30652 |
| 20931 | Bulk Product 6acb1f58 | Clothing | $750.21 | 48 | ★ 0.3 | 80 | BULK-9b297bea |
| 20932 | Bulk Product 9eeddccf | Books | $105.48 | 278 | ★ 4.4 | 64 | BULK-5c323c68 |
| 20933 | Bulk Product 0198b84e | Electronics | $312.32 | 89 | ★ 1.4 | 96 | BULK-c8972aa4 |
| 20934 | Bulk Product 839d3d0d | Toys & Games | $233.96 | 456 | ★ 2.1 | 8 | BULK-3ddf2d2c |
// 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
};
}