Paginate Optimizely Graph results using C# SDK
Retrieve specific pages of Optimizely Graph results, combine with filters and sorting, and include total counts for navigation.
Use .Skip() and .Limit() to retrieve a specific page of results. Add .IncludeTotal() to get the total count needed to render page navigation.
Skip and limit results
.Skip() sets how many items to skip. .Limit() sets how many to return:
var result = await client
.QueryContent<BlogPostPage>()
.Skip(2)
.Limit(3)
.GetAsContentAsync();This skips the first two items and returns the next three.
Calculate page offsets
For page-number-based navigation, calculate the skip value from the current page number and page size:
int pageSize = 10;
int currentPage = 1; // pages are 1-based
var result = await client
.QueryContent<BlogPostPage>()
.Skip((currentPage - 1) * pageSize)
.Limit(pageSize)
.IncludeTotal()
.GetAsContentAsync();
int totalPages = (int)Math.Ceiling((double)result.Total!.Value / pageSize);result.Total is null unless you call .IncludeTotal(). See Include total for details.
Combine pagination with filters and ordering
Pagination methods compose freely with filters, ordering, and other query methods:
var result = await client
.QueryContent<BlogPostPage>()
.Where(x => x.BlogCategory == "Tutorials")
.OrderBy(x => x.StartPublish, OrderDirection.Descending)
.Skip((currentPage - 1) * pageSize)
.Limit(pageSize)
.IncludeTotal()
.GetAsContentAsync();Updated 6 days ago
