{"record":{"id":"a6b04aadac855a3e","repo":"dotnet/efcore","slug":"reversing-the-ordering-is-not-supported-when-limit","errorCode":null,"errorMessage":"Reversing the ordering is not supported when limit or offset are already applied.","messagePattern":"Reversing the ordering is not supported when limit or offset are already applied\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs","lineNumber":508,"sourceCode":"\n        if (_orderings.FirstOrDefault(o => o.Expression.Equals(orderingExpression.Expression)) == null)\n        {\n            _orderings.Add(orderingExpression);\n        }\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public void ReverseOrderings()\n    {\n        if (Limit != null\n            || Offset != null)\n        {\n            throw new InvalidOperationException(CosmosStrings.ReverseAfterSkipTakeNotSupported);\n        }\n\n        var existingOrderings = _orderings.ToArray();\n\n        _orderings.Clear();\n\n        foreach (var existingOrdering in existingOrderings)\n        {\n            _orderings.Add(\n                new OrderingExpression(\n                    existingOrdering.Expression,\n                    !existingOrdering.IsAscending));\n        }\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs#L490-L526","documentation":"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.","triggerScenarios":".Skip(n).Take(m).Reverse(), .Take(n).Reverse(), or any query where Reverse() follows Skip/Take.","commonSituations":"Trying to flip a paginated, ordered sequence client-style; pagination helpers that append Reverse() unconditionally.","solutions":["Apply Reverse() before Skip/Take so the ordering is reversed at the source.","Materialize with AsEnumerable() and reverse on the client if the reversal genuinely must follow paging."],"exampleFix":"// before\nvar q = db.Items\n    .OrderByDescending(i => i.Date)\n    .Skip(10).Take(5)\n    .Reverse();\n\n// after - reverse the ordering direction before paging\nvar q = db.Items\n    .OrderBy(i => i.Date)\n    .Skip(10).Take(5);","handlingStrategy":"validation","validationCode":"// Ensure Reverse() is applied before Skip/Take at the query builder boundary.\nstatic bool IsReversedAfterPaging(IQueryable q) => /* inspect expression tree */ false;\nif (IsReversedAfterPaging(query)) throw new InvalidOperationException(\"Reverse before Skip/Take.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Order query operator application so Reverse precedes Skip/Take.","Switch the OrderBy direction instead of calling Reverse when paging follows.","Materialize before reversing if reversal must follow pagination."],"tags":["cosmos","ordering","paging"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}