litedb-org/LiteDB · error · NotSupportedException

Method {met.Method.Name} not available to convert to BsonExp

Error message

Method {met.Method.Name} not available to convert to BsonExpression inside Any/All call.

What it means

Thrown by VisitEnumerablePredicate when, inside an Any/All lambda, a method is called whose declaring type has no registered ITypeResolver. The resolver lookup (TryGetResolver) fails, so LiteDB has no way to translate the method into BsonExpression within the Any/All context.

Source

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

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

        }

        /// <summary>

View on GitHub (pinned to f906a5f850)

Solutions

  1. Restructure the query to Select a primitive (String, Int32, etc.) before Any/All so the method is on a resolver-backed type.
  2. Replace the method call with a direct comparison against a mapped field.
  3. Post-filter in memory if the operation cannot be expressed in BsonExpression.

Example fix

// before
col.Find(x => x.Items.Select(i => i.Tag).Any(t => t.IsMatch("x")));
// Tag type has no resolver

// after -- compare a mapped string field instead
col.Find(x => x.Items.Any(i => i.Tag == "x"));
Defensive patterns

Strategy: validation

Validate before calling

// Ensure methods inside Any/All are on resolver-backed types (String, Int32, etc.)
// Instead of: .Select(i => i.CustomObj).Any(t => t.IsMatch("x"))
// Use: .Any(i => i.Tag == "x") where Tag is a string

Try / catch

try
{
    var results = col.Find(x => x.Items.Select(i => i.Tag).Any(t => t.IsMatch("x")));
}
catch (NotSupportedException ex) when (ex.Message.Contains("not available to convert to BsonExpression inside Any/All"))
{
    results = col.Find(x => x.Items.Any(i => i.Tag == "x"));
}

Prevention

When it happens

Trigger: Inside an Any/All lambda, calling a method on a type that has no resolver. For example x.Items.Select(i => i.Tag).Any(t => t.IsMatch("x")) where the Tag type has no resolver.

Common situations: Calling custom methods on user-defined types within Any/All predicates. Assuming LiteDB can evaluate arbitrary .NET methods inside enumerable predicates.

Related errors


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