dotnet/efcore · error · InvalidOperationException

Reversing the ordering is not supported when limit or…

Error message

Reversing the ordering is not supported when limit or offset are already applied.

What it means

ReverseOrderings in Cosmos SelectExpression throws when Reverse() is invoked on a query that already has Limit (Take) or Offset (Skip) applied. Cosmos SQL cannot reverse orderings after pagination because the semantics of reverse+paged SQL are not well-defined at the SQL layer. Reverse must be applied before Skip/Take.

Solutions

  1. Apply Reverse() before Skip/Take so the ordering is reversed at the source.
  2. Materialize with AsEnumerable() and reverse on the client if the reversal genuinely must follow paging.

Example fix

// before
var q = db.Items
    .OrderByDescending(i => i.Date)
    .Skip(10).Take(5)
    .Reverse();

// after - reverse the ordering direction before paging
var q = db.Items
    .OrderBy(i => i.Date)
    .Skip(10).Take(5);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure Reverse() is applied before Skip/Take at the query builder boundary.
static bool IsReversedAfterPaging(IQueryable q) => /* inspect expression tree */ false;
if (IsReversedAfterPaging(query)) throw new InvalidOperationException("Reverse before Skip/Take.");

Prevention

When it happens

Trigger: .Skip(n).Take(m).Reverse(), .Take(n).Reverse(), or any query where Reverse() follows Skip/Take.

Common situations: Trying to flip a paginated, ordered sequence client-style; pagination helpers that append Reverse() unconditionally.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/a6b04aadac855a3e. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs:508

        if (_orderings.FirstOrDefault(o => o.Expression.Equals(orderingExpression.Expression)) == null)
        {
            _orderings.Add(orderingExpression);
        }
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public void ReverseOrderings()
    {
        if (Limit != null
            || Offset != null)
        {
            throw new InvalidOperationException(CosmosStrings.ReverseAfterSkipTakeNotSupported);
        }

        var existingOrderings = _orderings.ToArray();

        _orderings.Clear();

        foreach (var existingOrdering in existingOrderings)
        {
            _orderings.Add(
                new OrderingExpression(
                    existingOrdering.Expression,
                    !existingOrdering.IsAscending));
        }
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in

View on GitHub (pinned to 3a2006ef56)