litedb-org/LiteDB · error · ArgumentException

Multiple OrderBy calls are not supported. Use ThenBy for add

Error message

Multiple OrderBy calls are not supported. Use ThenBy for additional sort keys.

What it means

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.

Source

Thrown at LiteDB/Client/Database/LiteQueryable.cs:110

        /// <summary>
        /// Filters a sequence of documents based on a predicate expression
        /// </summary>
        public ILiteQueryable<T> Where(Expression<Func<T, bool>> predicate)
        {
            return this.Where(_mapper.GetExpression(predicate));
        }

        #endregion

        #region OrderBy

        /// <summary>
        /// Sort the documents of resultset in ascending (or descending) order according to a key.
        /// </summary>
        public ILiteQueryable<T> OrderBy(BsonExpression keySelector, int order = Query.Ascending)
        {
            if (_query.OrderBy.Count > 0) throw new ArgumentException("Multiple OrderBy calls are not supported. Use ThenBy for additional sort keys.");

            _query.OrderBy.Add(new QueryOrder(keySelector, order));
            return this;
        }

        /// <summary>
        /// Sort the documents of resultset in ascending (or descending) order according to a key (support only one OrderBy)
        /// </summary>
        public ILiteQueryable<T> OrderBy<K>(Expression<Func<T, K>> keySelector, int order = Query.Ascending)
        {
            return this.OrderBy(_mapper.GetExpression(keySelector), order);
        }

        /// <summary>
        /// Sort the documents of resultset in descending order according to a key.
        /// </summary>
        public ILiteQueryable<T> OrderByDescending(BsonExpression keySelector) => this.OrderBy(keySelector, Query.Descending);

View on GitHub (pinned to f906a5f850)

Solutions

  1. Replace the second OrderBy/OrderByDescending with ThenBy/ThenByDescending.
  2. If building sort keys dynamically, start with OrderBy for the first key and ThenBy for the rest.
  3. If you need to re-sort, construct a fresh query instead of chaining.

Example fix

// before
var q = col.Query().OrderBy("$.LastName").OrderBy("$.FirstName");

// after
var q = col.Query().OrderBy("$.LastName").ThenBy("$.FirstName");
Defensive patterns

Strategy: validation

Validate before calling

// Before sorting, decide which key is primary and which are secondary.
// Use OrderBy once, then ThenBy for the rest.
var q = col.Query().OrderBy("$.LastName").ThenBy("$.FirstName");

Try / catch

try { q.OrderBy(key); }
catch (ArgumentException ex) when (ex.Message.Contains("ThenBy"))
{
    // switch the caller to ThenBy and retry the composition
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/687a2ba31f67a6e8. Report an issue: GitHub.