litedb-org/LiteDB · error · NotSupportedException

List initializers {initializer.AddMethod.Name} not supported

Error message

List initializers {initializer.AddMethod.Name} not supported when convert to BsonExpression ({node}).

What it means

Thrown when a List<T> initializer for a DbRef list property uses a collection Add method whose name is not 'Add' or whose argument count is not exactly 1. LiteDB only knows how to translate the standard List<T>.Add(T) single-argument pattern into a BsonExpression array.

Source

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

                // new List<T> { new BsonRefId<T>, ... }
                case ListInitExpression { Type: { IsConstructedGenericType: true, GenericTypeArguments.Length: 1 } } expr
                    when !isInList && expr.Type.GetGenericTypeDefinition() == typeof(List<>) && memberMapper.UnderlyingType.IsAssignableFrom(expr.Type.GetGenericArguments()[0]):

                    _builder.Append("[ ");

                    for (var i = 0; i < expr.Initializers.Count; i++)
                    {
                        if (i > 0)
                        {
                            _builder.Append(", ");
                        }

                        var initializer = expr.Initializers[i];

                        if (initializer.Arguments.Count != 1 || initializer.AddMethod.Name != "Add")
                        {
                            throw new NotSupportedException($"List initializers {initializer.AddMethod.Name} not supported when convert to BsonExpression ({node}).");
                        }

                        if (!TryVisitDbRefIdExpression(initializer.Arguments[0], memberMapper, true))
                        {
                            throw new NotSupportedException($"Expression {expr} not supported for BsonRefId<T>.");
                        }
                    }

                    _builder.Append(" ]");
                    return true;

                default:
                    return false;
            }
        }

        /// <summary>
        /// Resolves and writes `new BsonRefId&lt;T&gt;(id)` into the _builder.

View on GitHub (pinned to f906a5f850)

Solutions

  1. Use a plain List<T> with standard Add semantics: new List<Product> { new BsonRefId<Product>(id) }.
  2. If you need a custom collection type on the model, keep the LINQ update expression using List<T> and let the mapper handle the conversion.
  3. Avoid multi-argument Add patterns in DbRef list initializers.

Example fix

// before (AddRange or custom Add)
x => new Order
{
    Products = new MyCustomList<Product> { new BsonRefId<Product>(id) }
}
// after
x => new Order
{
    Products = new List<Product> { new BsonRefId<Product>(id) }
}
Defensive patterns

Strategy: validation

Validate before calling

// Ensure DbRef list properties use List<T> for LINQ expressions
// If the model uses a custom collection, switch the LINQ expression to List<T>
// e.g.: new List<Product> { new BsonRefId<Product>(id) } instead of new CustomCollection<Product> { ... }

Try / catch

try
{
    col.UpdateMany(
        x => new Order { Products = new List<Product> { new BsonRefId<Product>(id) } },
        x => x.Id == orderId);
}
catch (NotSupportedException ex) when (ex.Message.Contains("List initializers"))
{
    // The Add method was not standard List<T>.Add(T). Use List<T>.
    throw;
}

Prevention

When it happens

Trigger: Using a custom collection or a List subclass whose initializer calls a method other than Add (e.g., AddRange, Enqueue, Push), or an Add overload that takes multiple arguments. The ListInitExpression is matched but the initializer's AddMethod.Name check fails.

Common situations: Using a type like ObservableCollection or a custom list with Insert/AddrRange in a collection initializer. Compiler-generated initializers for non-standard Add patterns (e.g., dictionary Add(key, value)).

Related errors


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