{"record":{"id":"687a2ba31f67a6e8","repo":"litedb-org/LiteDB","slug":"multiple-orderby-calls-are-not-supported-use-then","errorCode":null,"errorMessage":"Multiple OrderBy calls are not supported. Use ThenBy for additional sort keys.","messagePattern":"Multiple OrderBy calls are not supported\\. Use ThenBy for additional sort keys\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Database/LiteQueryable.cs","lineNumber":110,"sourceCode":"\n        /// <summary>\n        /// Filters a sequence of documents based on a predicate expression\n        /// </summary>\n        public ILiteQueryable<T> Where(Expression<Func<T, bool>> predicate)\n        {\n            return this.Where(_mapper.GetExpression(predicate));\n        }\n\n        #endregion\n\n        #region OrderBy\n\n        /// <summary>\n        /// Sort the documents of resultset in ascending (or descending) order according to a key.\n        /// </summary>\n        public ILiteQueryable<T> OrderBy(BsonExpression keySelector, int order = Query.Ascending)\n        {\n            if (_query.OrderBy.Count > 0) throw new ArgumentException(\"Multiple OrderBy calls are not supported. Use ThenBy for additional sort keys.\");\n\n            _query.OrderBy.Add(new QueryOrder(keySelector, order));\n            return this;\n        }\n\n        /// <summary>\n        /// Sort the documents of resultset in ascending (or descending) order according to a key (support only one OrderBy)\n        /// </summary>\n        public ILiteQueryable<T> OrderBy<K>(Expression<Func<T, K>> keySelector, int order = Query.Ascending)\n        {\n            return this.OrderBy(_mapper.GetExpression(keySelector), order);\n        }\n\n        /// <summary>\n        /// Sort the documents of resultset in descending order according to a key.\n        /// </summary>\n        public ILiteQueryable<T> OrderByDescending(BsonExpression keySelector) => this.OrderBy(keySelector, Query.Descending);\n","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Database/LiteQueryable.cs#L92-L128","documentation":"Thrown by LiteQueryable.OrderBy when _query.OrderBy already contains an entry. LiteDB permits only a single OrderBy call per query; additional sort keys must be appended via ThenBy/ThenByDescending, which add to the same OrderBy list without this guard. Calling OrderBy a second time is treated as a programming error, not a valid re-sort.","triggerScenarios":"Chaining two OrderBy calls: col.Query().OrderBy(\"$.Name\").OrderBy(\"$.Age\"), or col.Query().OrderBy(x => x.Name).OrderByDescending(x => x.Age). The expression-based overloads delegate to the BsonExpression overload and hit the same guard.","commonSituations":"Porting code from LINQ-to-Objects where multiple OrderBy calls are idiomatic, refactoring a sort builder that concatenates OrderBy calls, or copy-pasting a sort clause into an already-sorted query.","solutions":["Replace the second OrderBy/OrderByDescending with ThenBy/ThenByDescending.","If building sort keys dynamically, start with OrderBy for the first key and ThenBy for the rest.","If you need to re-sort, construct a fresh query instead of chaining."],"exampleFix":"// before\nvar q = col.Query().OrderBy(\"$.LastName\").OrderBy(\"$.FirstName\");\n\n// after\nvar q = col.Query().OrderBy(\"$.LastName\").ThenBy(\"$.FirstName\");","handlingStrategy":"validation","validationCode":"// Before sorting, decide which key is primary and which are secondary.\n// Use OrderBy once, then ThenBy for the rest.\nvar q = col.Query().OrderBy(\"$.LastName\").ThenBy(\"$.FirstName\");","typeGuard":null,"tryCatchPattern":"try { q.OrderBy(key); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"ThenBy\"))\n{\n    // switch the caller to ThenBy and retry the composition\n}","preventionTips":["Treat the first sort as OrderBy and every subsequent one as ThenBy by convention.","When building sorts dynamically, track whether the first key was already added."],"tags":["query","orderby","linq","argument"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}