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

Order Optimizely Graph results with the C# SDK

Order Optimizely Graph results in the C# SDK with .OrderBy() and .ThenBy().

Use .OrderBy() to sort results by any indexed field. Chain .ThenBy() for secondary sorts.

OrderDirection is an enum in the Optimizely.Graph.Cms.Query.Implementation namespace.

Sort ascending

Return results from lowest to highest value for the specified field.

await client
    .QueryContent<BlogPostPage>()
    .OrderBy(x => x.StartPublish, OrderDirection.Ascending)
    .GetAsContentAsync();

Sort descending

Return results from highest to lowest value for the specified field.

await client
    .QueryContent<BlogPostPage>()
    .OrderBy(x => x.StartPublish, OrderDirection.Descending)
    .GetAsContentAsync();

Sort by multiple fields

Use .ThenBy() to apply a secondary sort when primary values are equal:

await client
    .QueryContent<BlogPostPage>()
    .OrderBy(x => x.BlogCategory, OrderDirection.Ascending)
    .ThenBy(x => x.StartPublish, OrderDirection.Descending)
    .GetAsContentAsync();

Results sort alphabetically by category, then by newest publish date within each category.

Sort by numeric fields

Sort by any indexed numeric property, such as a rating or view count.

await client
    .QueryContent<BlogPostPage>()
    .OrderBy(x => x.Rating, OrderDirection.Descending)
    .GetAsContentAsync();

Combine sort with filters and pagination

.OrderBy() composes freely with .Where(), .Skip(), .Limit(), and other query methods:

await client
    .QueryContent<BlogPostPage>()
    .Where(x => x.BlogCategory == "Tutorials")
    .OrderBy(x => x.StartPublish, OrderDirection.Descending)
    .Limit(10)
    .GetAsContentAsync();