{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs#L490-L526","documentation":"SelectExpression.ReverseOrderings throws InvalidOperationException when Reverse() is applied after a Limit (Take) or Offset (Skip) has already been set. Reversing an already-paged ordering cannot be expressed in Cosmos SQL without changing semantics, so the provider rejects it.","triggerScenarios":"Chaining `.OrderBy(...).Skip(n).Take(m).Reverse()` or `.Take(m).Reverse()` on a Cosmos query, where Skip/Take precede Reverse in the translation order.","commonSituations":"Building a 'last N items' query by reversing a paged ascending result; porting a SQL pattern that relied on reverse-after-paging.","solutions":["Apply Reverse before Skip/Take, or equivalently order descending from the start instead of reversing after paging.","Switch the original OrderBy direction (OrderByDescending) and keep paging, avoiding Reverse entirely.","Perform the reversal client-side after materializing the page."],"exampleFix":"// before\nvar q = ctx.Logs.OrderBy(l => l.Time)\n    .Skip(0).Take(10).Reverse();\n\n// after (page the descending order directly)\nvar q = ctx.Logs.OrderByDescending(l => l.Time)\n    .Skip(0).Take(10);","handlingStrategy":"validation","validationCode":"// Guard against Reverse after paging by reordering the query construction.\nstatic IQueryable<T> Paged<T, K>(IQueryable<T> q, Expression<Func<T,K>> key, int skip, int take, bool desc)\n    => (desc ? q.OrderByDescending(key) : q.OrderBy(key)).Skip(skip).Take(take);","typeGuard":null,"tryCatchPattern":"try { var q = query.Reverse().ToList(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Reversing the ordering\"))\n{ /* reverse before Skip/Take, or order descending */ }","preventionTips":["Order descending instead of reversing a paged ascending result.","Apply Reverse before Skip/Take, never after.","Centralize paging in a helper that prevents reverse-after-page."],"tags":["cosmos","ordering","paging","reverse"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}