{"record":{"id":"afec563afa761a1b","repo":"litedb-org/LiteDB","slug":"list-initializers-initializer-addmethod-name-not","errorCode":null,"errorMessage":"List initializers {initializer.AddMethod.Name} not supported when convert to BsonExpression ({node}).","messagePattern":"List initializers (.+?) not supported when convert to BsonExpression \\((.+?)\\)\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs","lineNumber":818,"sourceCode":"\n                // new List<T> { new BsonRefId<T>, ... }\n                case ListInitExpression { Type: { IsConstructedGenericType: true, GenericTypeArguments.Length: 1 } } expr\n                    when !isInList && expr.Type.GetGenericTypeDefinition() == typeof(List<>) && memberMapper.UnderlyingType.IsAssignableFrom(expr.Type.GetGenericArguments()[0]):\n\n                    _builder.Append(\"[ \");\n\n                    for (var i = 0; i < expr.Initializers.Count; i++)\n                    {\n                        if (i > 0)\n                        {\n                            _builder.Append(\", \");\n                        }\n\n                        var initializer = expr.Initializers[i];\n\n                        if (initializer.Arguments.Count != 1 || initializer.AddMethod.Name != \"Add\")\n                        {\n                            throw new NotSupportedException($\"List initializers {initializer.AddMethod.Name} not supported when convert to BsonExpression ({node}).\");\n                        }\n\n                        if (!TryVisitDbRefIdExpression(initializer.Arguments[0], memberMapper, true))\n                        {\n                            throw new NotSupportedException($\"Expression {expr} not supported for BsonRefId<T>.\");\n                        }\n                    }\n\n                    _builder.Append(\" ]\");\n                    return true;\n\n                default:\n                    return false;\n            }\n        }\n\n        /// <summary>\n        /// Resolves and writes `new BsonRefId&lt;T&gt;(id)` into the _builder.","sourceCodeStart":800,"sourceCodeEnd":836,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs#L800-L836","documentation":"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.","triggerScenarios":"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.","commonSituations":"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)).","solutions":["Use a plain List<T> with standard Add semantics: new List<Product> { new BsonRefId<Product>(id) }.","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.","Avoid multi-argument Add patterns in DbRef list initializers."],"exampleFix":"// before (AddRange or custom Add)\nx => new Order\n{\n    Products = new MyCustomList<Product> { new BsonRefId<Product>(id) }\n}\n// after\nx => new Order\n{\n    Products = new List<Product> { new BsonRefId<Product>(id) }\n}","handlingStrategy":"validation","validationCode":"// Ensure DbRef list properties use List<T> for LINQ expressions\n// If the model uses a custom collection, switch the LINQ expression to List<T>\n// e.g.: new List<Product> { new BsonRefId<Product>(id) } instead of new CustomCollection<Product> { ... }","typeGuard":null,"tryCatchPattern":"try\n{\n    col.UpdateMany(\n        x => new Order { Products = new List<Product> { new BsonRefId<Product>(id) } },\n        x => x.Id == orderId);\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"List initializers\"))\n{\n    // The Add method was not standard List<T>.Add(T). Use List<T>.\n    throw;\n}","preventionTips":["Use System.Collections.Generic.List<T> for DbRef list properties in LINQ update expressions.","Avoid custom collections with non-standard Add methods in LINQ expressions sent to LiteDB.","If the model requires a custom collection type, map it to List<T> in the LINQ expression portion."],"tags":["linq","dbref","bsonrefid","list-initializer"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}