litedb-org/LiteDB · error · LiteException

0

0

Error message

Any/All requires simple parameter on left side. Eg: `x => x.Phones.Select(p => p.Number).Any(n => n > 5)`

What it means

Thrown by VisitEnumerablePredicate when the body of an Any/All lambda is a BinaryExpression whose left operand is not a bare ParameterExpression. LiteDB's Any/All translation requires the comparison to be directly against the lambda parameter (e.g., n => n > 5), not against a member of it (e.g., n => n.Value > 5). The fix is to flatten with Select first.

Source

Thrown at LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs:565

                else
                {
                    _builder.Append(token.Type == TokenType.String ? "'" + token.Value + "'" : token.Value);
                }
            }
        }

        /// <summary>
        /// Resolve Enumerable predicate when using Any/All enumerable extensions
        /// </summary>
        private void VisitEnumerablePredicate(LambdaExpression lambda)
        {
            var expression = lambda.Body;

            // Visit .Any(x => `x == 10`)
            if (expression is BinaryExpression bin)
            {
                // requires only parameter in left side
                if (bin.Left.NodeType != ExpressionType.Parameter) throw new LiteException(0, "Any/All requires simple parameter on left side. Eg: `x => x.Phones.Select(p => p.Number).Any(n => n > 5)`");

                var op = this.GetOperator(bin.NodeType);

                _builder.Append(op);

                this.VisitAsPredicate(bin.Right, false);
            }
            // Visit .Any(x => `x.StartsWith("John")`)
            else if (expression is MethodCallExpression met)
            {
                // requires only parameter in left side
                if (met.Object.NodeType != ExpressionType.Parameter) throw new NotSupportedException("Any/All requires simple parameter on left side. Eg: `x.Customers.Select(c => c.Name).Any(n => n.StartsWith('J'))`");

                // if not found in resolver, try run method
                if (!TryGetResolver(met.Method.DeclaringType, out var type))
                {
                    throw new NotSupportedException($"Method {met.Method.Name} not available to convert to BsonExpression inside Any/All call.");
                }

View on GitHub (pinned to f906a5f850)

Solutions

  1. Insert a .Select(p => p.Field) before .Any(n => n > value) so the Any lambda compares the parameter directly.
  2. Restructure the query so the Any/All lambda body is a direct binary comparison against the lambda parameter.

Example fix

// before
col.Find(x => x.Items.Any(i => i.Price > 100));
// throws: Any/All requires simple parameter on left side

// after -- Select the field first
col.Find(x => x.Items.Select(i => i.Price).Any(p => p > 100));
Defensive patterns

Strategy: validation

Validate before calling

// Ensure Any/All lambda bodies are direct comparisons against the parameter
// Correct: .Select(p => p.Field).Any(n => n > value)
// Incorrect: .Any(i => i.Field > value)

Type guard

// Check that an Any/All lambda body is a direct parameter comparison
static bool IsSimpleAnyAllPredicate<T, K>(Expression<Func<T, K>> expr)
{
    if (expr.Body is BinaryExpression bin)
        return bin.Left.NodeType == ExpressionType.Parameter;
    return false;
}

Try / catch

try
{
    var results = col.Find(x => x.Items.Any(i => i.Price > 100));
}
catch (LiteException ex) when (ex.Message.Contains("Any/All requires simple parameter on left side"))
{
    results = col.Find(x => x.Items.Select(i => i.Price).Any(p => p > 100));
}

Prevention

When it happens

Trigger: Writing x.Items.Any(i => i.Price > 100) where the left side i.Price is a member access, not the parameter i itself. The Any/All predicate body is a BinaryExpression and bin.Left.NodeType is not ExpressionType.Parameter.

Common situations: Intuitively writing .Any(lambda) on a collection of complex objects and comparing a property inside the lambda, without first selecting the property. Assuming Any/All supports full nested member comparisons like EF Core does.

Related errors


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