Include the total count in Optimizely Graph queries with the C# SDK
4 min
Call .IncludeTotal() to get the total number of items matching your query. Without it, result.Total is null.
Get the total count for a query
Retrieve the total number of matching items alongside query results.
var result = await client
.QueryContent<BlogPostPage>()
.IncludeTotal()
.GetAsContentAsync();
int total = result.Total!.Value;Use the total count for pagination
Use the total count to calculate the number of pages:
int pageSize = 10;
int currentPage = 1;
var result = await client
.QueryContent<BlogPostPage>()
.Skip((currentPage - 1) * pageSize)
.Limit(pageSize)
.IncludeTotal()
.GetAsContentAsync();
int totalPages = (int)Math.Ceiling((double)result.Total!.Value / pageSize);Total reflects active filters
The count reflects the filtered result set, not the full collection:
var result = await client
.QueryContent<BlogPostPage>()
.Where(x => x.IsFeatured == true)
.IncludeTotal()
.GetAsContentAsync();
// Total is the count of featured posts only, not all blog posts
var featuredTotal = result.Total;Total is consistent across pages
Fetch the total on page 1 and reuse it for all subsequent page navigation. It does not change between paginated calls for the same query:
// Page 1: capture total here
var page1 = await client
.QueryContent<BlogPostPage>()
.Skip(0)
.Limit(10)
.IncludeTotal()
.GetAsContentAsync();
int total = page1.Total!.Value; // Use this for all page navigation
// Page 2: total is the same
var page2 = await client
.QueryContent<BlogPostPage>()
.Skip(10)
.Limit(10)
.IncludeTotal()
.GetAsContentAsync();For pagination patterns that use this total, see Paginate Optimizely Graph results with the C# SDK.