{"record":{"id":"d03b9b8fcd58f244","repo":"litedb-org/LiteDB","slug":"expression-expr-not-supported-for-bsonrefid-t","errorCode":null,"errorMessage":"Expression {expr} not supported for BsonRefId<T>.","messagePattern":"Expression (.+?) not supported for BsonRefId<T>\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs","lineNumber":794,"sourceCode":"\n                    return false;\n\n                // new T[] { new BsonRefId<T>, ... }\n                case NewArrayExpression expr\n                    when !isInList && expr.Type.IsArray && memberMapper.UnderlyingType.IsAssignableFrom(expr.Type.GetElementType()):\n\n                    _builder.Append(\"[ \");\n\n                    for (var i = 0; i < expr.Expressions.Count; i++)\n                    {\n                        if (i > 0)\n                        {\n                            _builder.Append(\", \");\n                        }\n\n                        if (!TryVisitDbRefIdExpression(expr.Expressions[i], 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                // 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                        }","sourceCodeStart":776,"sourceCodeEnd":812,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs#L776-L812","documentation":"Thrown when translating an array initializer for a DbRef array property (e.g., Product[] Products) where one of the array elements is not a valid BsonRefId<T> expression. The visitor recursed into TryVisitDbRefIdExpression for that element and it returned false, meaning the expression shape was unrecognized.","triggerScenarios":"Writing a LINQ UpdateMany/Insert expression like x => new Order { Products = new[] { someProductInstance } } where the array element is a direct entity reference or an unsupported expression instead of new BsonRefId<Product>(id).","commonSituations":"Forgetting to wrap array element IDs in BsonRefId<T> when doing bulk reference updates. Mixing actual entity instances with BsonRefId<T> in the same array initializer. Using new BsonRefId<WrongType>(id) where the type is not assignable to the array element type.","solutions":["Wrap every array element in BsonRefId<T>: Products = new[] { new BsonRefId<Product>(id1), new BsonRefId<Product>(id2) }.","Ensure the BsonRefId generic argument matches (or is assignable to) the array element type.","Do not mix raw entity instances with BsonRefId<T> in the same initializer."],"exampleFix":"// before\nx => new Order\n{\n    Products = new[] { existingProduct }\n}\n// after\nx => new Order\n{\n    Products = new[] { new BsonRefId<Product>(existingProduct.Id) }\n}","handlingStrategy":"validation","validationCode":"// Ensure all array elements in a DbRef update expression are BsonRefId<T>\n// No runtime API; validate at code-review time that each element uses new BsonRefId<T>(id)\n// e.g.: Products = new[] { new BsonRefId<Product>(p1Id), new BsonRefId<Product>(p2Id) }","typeGuard":null,"tryCatchPattern":"try\n{\n    col.UpdateMany(\n        x => new Order { Products = new[] { new BsonRefId<Product>(id1), new BsonRefId<Product>(id2) } },\n        x => x.Id == orderId);\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"not supported for BsonRefId\"))\n{\n    // Review the array initializer: every element must be new BsonRefId<T>(id)\n    throw;\n}","preventionTips":["Always use new BsonRefId<ElementType>(id) for every element in DbRef array initializers.","Never place raw entity instances in DbRef array/list initializers within LINQ expressions.","Write integration tests for UpdateMany expressions involving DbRef arrays to catch translation failures early."],"tags":["linq","dbref","bsonrefid","array-initializer"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}