HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Include total count in Optimizely Graph queries

Retrieve the total count of matching items in Content Graph queries to support pagination and filtered results.

👍

Early access preview

This content is an early access preview and may change before general availability. Use the thumbs up or down at the end of this article to share feedback and help shape the final release.

Call .IncludeTotal() to get the total number of items matching your query. Without it, result.Total is null.

Basic usage

var result = await client
    .QueryContent<BlogPostPage>()
    .IncludeTotal()
    .GetAsContentAsync();

int total = result.Total!.Value;

With pagination

The total count is what you need 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 always 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

You can safely fetch the total on page 1 and reuse it for all subsequent page navigation — it won't 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 will be the same
var page2 = await client
    .QueryContent<BlogPostPage>()
    .Skip(10)
    .Limit(10)
    .IncludeTotal()
    .GetAsContentAsync();