litedb-org/LiteDB · error · NotSupportedException

Method {node.Method.Name} not available to convert to BsonEx

Error message

Method {node.Method.Name} not available to convert to BsonExpression ({node.ToString()}).

What it means

Thrown by VisitMethodCall when the method's declaring type has NO registered resolver and the method is called on a parameter expression (isParam is true). LiteDB cannot translate a method call that operates on a document field if it does not know how to resolve the declaring type. If the method is NOT on a parameter (e.g., a closure constant), the visitor instead compiles and evaluates it in memory.

Source

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

            var hasResolver = TryGetResolver(node.Method.DeclaringType, out var type);

            if (node.Method.DeclaringType == typeof(Enumerable) && node.Arguments.Count > 0)
            {
                var first = node.Arguments[0].Type;

                if (first.IsGenericType && first.GetGenericTypeDefinition() == typeof(IGrouping<,>))
                {
                    type = _resolver[typeof(IGrouping<,>)];
                    hasResolver = true;
                }
            }

            if (!hasResolver)
            {
                // if method are called by parameter expression and it's not exists, throw error
                var isParam = ParameterExpressionVisitor.Test(node);

                if (isParam) throw new NotSupportedException($"Method {node.Method.Name} not available to convert to BsonExpression ({node.ToString()}).");

                // otherwise, try compile and execute
                var value = this.Evaluate(node);

                base.Visit(Expression.Constant(value));

                return node;
            }

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

            if (pattern == null) throw new NotSupportedException($"Method {Reflection.MethodName(node.Method)} in {node.Method.DeclaringType.Name} are not supported when convert to BsonExpression ({node.ToString()}).");

            // run pattern using object as # and args as @n
            this.ResolvePattern(pattern, node.Object, node.Arguments);

            return node;

View on GitHub (pinned to f906a5f850)

Solutions

  1. Rewrite the query to use only supported methods and member comparisons (e.g., compare a mapped field directly instead of calling a method).
  2. If the method result is constant relative to the query, hoist it into a closure variable so the visitor evaluates it in memory rather than trying to translate it.
  3. Register a custom ITypeResolver for the type if translation is genuinely needed.
  4. Fall back to a raw BsonExpression or post-query in-memory filtering.

Example fix

// before
var active = col.Find(x => x.Status.IsActive());
// Status has no resolver

// after -- compare a mapped primitive field instead
var active = col.Find(x => x.Status == "Active");
Defensive patterns

Strategy: validation

Validate before calling

// Hoist method results that are constant relative to the query into closure variables
var computedValue = myCustomObj.DoSomething(); // evaluate in memory
col.Find(x => x.Field == computedValue);

Try / catch

try
{
    var results = col.Find(x => x.CustomObj.DoSomething() == true);
}
catch (NotSupportedException ex) when (ex.Message.Contains("not available to convert to BsonExpression"))
{
    var val = customObj.DoSomething();
    results = col.Find(x => x.Field == val);
}

Prevention

When it happens

Trigger: Calling an instance method or extension method on a query parameter where the method's declaring type has no ITypeResolver. For example x => x.CustomObj.DoSomething() where CustomObj is a user type with no resolver, or x => x.Items.MyExtension() where MyExtension is a custom extension method.

Common situations: Using custom extension methods or instance methods on user-defined types inside LINQ queries. Assuming LiteDB can translate any .NET method call. Mixing domain-logic methods into query predicates.

Related errors


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