{"record":{"id":"c0900f935354a1b5","repo":"dotnet/efcore","slug":"ordering-using-a-scoring-function-is-mutually-excl","errorCode":null,"errorMessage":"Ordering using a scoring function is mutually exclusive with other forms of ordering.","messagePattern":"Ordering using a scoring function is mutually exclusive with other forms of ordering\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs","lineNumber":484,"sourceCode":"        _orderings.Clear();\n        _orderings.Add(orderingExpression);\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 AppendOrdering(OrderingExpression orderingExpression)\n    {\n        if (_orderings.Count > 0)\n        {\n            var existingScoringFunctionOrdering = _orderings is [{ Expression: SqlFunctionExpression { IsScoringFunction: true } }];\n            var appendingScoringFunctionOrdering = orderingExpression.Expression is SqlFunctionExpression { IsScoringFunction: true };\n            if (appendingScoringFunctionOrdering || existingScoringFunctionOrdering)\n            {\n                throw new InvalidOperationException(\n                    appendingScoringFunctionOrdering && existingScoringFunctionOrdering\n                        ? CosmosStrings.OrderByMultipleScoringFunctionWithoutRrf(nameof(CosmosDbFunctionsExtensions.Rrf))\n                        : CosmosStrings.OrderByScoringFunctionMixedWithRegularOrderby);\n            }\n        }\n\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>","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs#L466-L502","documentation":"AppendOrdering in Cosmos SelectExpression throws OrderByScoringFunctionMixedWithRegularOrderby when exactly one of the existing and appended orderings is a scoring function. Cosmos treats scoring-function ordering as mutually exclusive with regular property ordering; you cannot ThenBy a property on top of a VectorDistance/FullTextScore ordering or vice-versa.","triggerScenarios":".OrderBy(EF.Functions.VectorDistance(...)).ThenBy(x => x.Name) or .OrderBy(x => x.Name).ThenBy(EF.Functions.VectorDistance(...)).","commonSituations":"Adding a deterministic tiebreaker property (Name, Date, Id) alongside a scoring-function ordering.","solutions":["Remove the non-scoring ThenBy/OrderBy clause.","Apply the secondary property ordering client-side after materializing the ranked page."],"exampleFix":"// before\nvar q = db.Items\n    .OrderBy(i => EF.Functions.VectorDistance(i.Embedding, v))\n    .ThenBy(i => i.Name);\n\n// after - drop the tiebreaker from the SQL query\nvar q = db.Items\n    .OrderBy(i => EF.Functions.VectorDistance(i.Embedding, v));","handlingStrategy":"validation","validationCode":"// Reject mixing scoring and non-scoring orderings.\nbool hasScoring = orderings.Any(o => IsScoringExpression(o.Expression));\nbool hasRegular = orderings.Any(o => !IsScoringExpression(o.Expression));\nif (hasScoring && hasRegular) throw new InvalidOperationException(\"Do not mix scoring and property orderings.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When using a scoring-function ordering, do not add property ThenBy clauses.","Apply tiebreakers client-side after materializing the page.","Encapsulate ranking query construction in a helper that prevents mixed orderings."],"tags":["cosmos","ordering","scoring-function"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}