litedb-org/LiteDB · error · NotSupportedException

Any/All requires simple parameter on left side. Eg: `x.Custo

Error message

Any/All requires simple parameter on left side. Eg: `x.Customers.Select(c => c.Name).Any(n => n.StartsWith('J'))`

What it means

Thrown by VisitEnumerablePredicate when the body of an Any/All lambda is a MethodCallExpression but the method's Object (the instance the method is called on) is not a bare ParameterExpression. For example .Any(n => n.Name.ToUpper()) fails because n.Name is a member access, not the parameter n. The method must be called directly on the lambda parameter.

Source

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

            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.");
                }

                // otherwise I have resolver for this method
                var pattern = type.ResolveMethod(met.Method);

                if (pattern == null || !pattern.StartsWith("#")) throw new NotSupportedException($"Method {met.Method.Name} not available to convert to BsonExpression inside Any/All call.");

                // call resolve pattern removing first `#`
                this.ResolvePattern(pattern.Substring(1), met.Object, met.Arguments);
            }
            else
            {
                throw new LiteException(0, "When using Any/All method test do only simple predicate variable. Eg: `x => x.Phones.Select(p => p.Number).Any(n => n > 5)`");

View on GitHub (pinned to f906a5f850)

Solutions

  1. Ensure the method inside the Any/All lambda is called directly on the lambda parameter (e.g., .Select(p => p.Name).Any(n => n.StartsWith("A"))).
  2. Flatten the collection with Select to surface the primitive before applying Any/All.

Example fix

// before
col.Find(x => x.Users.Any(u => u.Name.ToUpper() == "JOHN"));

// after -- Select the field first
col.Find(x => x.Users.Select(u => u.Name).Any(n => n.ToUpper() == "JOHN"));
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the method inside Any/All is called directly on the parameter
// Correct: .Select(p => p.Name).Any(n => n.StartsWith("A"))
// Incorrect: .Any(u => u.Name.ToUpper().StartsWith("A"))

Type guard

// Check that a method inside Any/All is called on the bare parameter
static bool IsAnyAllMethodOnParameter(LambdaExpression lambda)
{
    if (lambda.Body is MethodCallExpression mce)
        return mce.Object?.NodeType == ExpressionType.Parameter;
    return false;
}

Try / catch

try
{
    var results = col.Find(x => x.Users.Any(u => u.Name.ToUpper() == "JOHN"));
}
catch (NotSupportedException ex) when (ex.Message.Contains("Any/All requires simple parameter"))
{
    results = col.Find(x => x.Users.Select(u => u.Name).Any(n => n.ToUpper() == "JOHN"));
}

Prevention

When it happens

Trigger: Writing x.Users.Select(u => u.Name).Any(n => n.ToUpper() == "JOHN") where the method is called on something other than the bare parameter. Specifically, met.Object.NodeType is not ExpressionType.Parameter.

Common situations: Chaining methods on a member inside an Any/All lambda (e.g., n.Value.ToString()). Forgetting to Select the target field before calling Any/All, so the method call is on a nested member rather than the lambda parameter.

Related errors


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