{"record":{"id":"be9375e7f6adffd4","repo":"dotnet/efcore","slug":"couldn-t-find-single-add-method-on-type-type-nam","errorCode":null,"errorMessage":"Couldn't find single Add method on type '{type.Name}', required for list initializer","messagePattern":"Couldn't find single Add method on type '(.+?)', required for list initializer","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":882,"sourceCode":"                            _ => throw new InvalidOperationException(\n                                $\"ObjectCreation: unsupported initializer for member of type '{lValueSymbol?.GetType().Name}': {e}\")\n                        };\n\n                        return memberInfo is null\n                            ? throw new InvalidOperationException(\n                                $\"ObjectCreation: couldn't find initialized member '{lValueSymbol.Name}': {e}\")\n                            : Bind(memberInfo, Visit(value));\n                    }));\n\n            // Non-assignment initializer => list initializer (new List<int> { 1, 2, 3 })\n            default:\n                // Find the correct Add() method on the collection type\n                // TODO: This doesn't work if there are multiple Add() methods (contrived). Complete solution would be to find the base\n                // TODO: type for all initializer expressions and find an Add overload of that type (or a superclass thereof)\n                var addMethod = type.GetMethods().SingleOrDefault(m => m.Name == \"Add\" && m.GetParameters().Length == 1);\n                if (addMethod is null)\n                {\n                    throw new InvalidOperationException(\n                        $\"Couldn't find single Add method on type '{type.Name}', required for list initializer\");\n                }\n\n                // TODO: Dictionary initializer, where each ElementInit has more than one expression\n\n                return ListInit(\n                    newExpression,\n                    objectCreation.Initializer.Expressions.Select(e => ElementInit(addMethod, Visit(e))));\n        }\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public override Expression VisitParenthesizedExpression(ParenthesizedExpressionSyntax parenthesized)","sourceCodeStart":864,"sourceCodeEnd":900,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L864-L900","documentation":"For a collection initializer (new List<T> { a, b }) the translator looks for exactly one single-parameter Add method via SingleOrDefault. If no single-arg Add exists, or if multiple exist (which makes SingleOrDefault itself throw), the initializer cannot be translated to a ListInit.","triggerScenarios":"A collection initializer on a type with no single-argument Add (e.g. dictionaries needing Add(key, value), or ConcurrentDictionary which exposes TryAdd not Add), or on a custom collection with several single-arg Add overloads.","commonSituations":"Dictionary initializers new Dictionary<K,V> { { k, v } }; custom collections with multiple Add(T) overloads; initializing ConcurrentDictionary or other collections without a unique Add.","solutions":["For dictionaries, construct empty and add entries outside the query, or use index-initializer syntax where supported","Avoid collection initializers on types with multiple single-arg Add overloads","Use a concrete List<T> for the initializer","Hoist the collection construction out of the precompiled query"],"exampleFix":"// before\nvar map = new Dictionary<int, string> { { 1, \"a\" }, { 2, \"b\" } };\n// after (built outside the query)\nvar map = new Dictionary<int, string>();\nmap.Add(1, \"a\"); map.Add(2, \"b\");","handlingStrategy":"validation","validationCode":"// Confirm the collection type has exactly one single-arg Add before using a collection initializer.\nvar adds = typeof(MyCollection).GetMethods().Where(m => m.Name == \"Add\" && m.GetParameters().Length == 1).ToArray();\nif (adds.Length != 1) { /* build the collection outside the query */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid collection initializers on dictionaries or types with multiple Add overloads inside queries","Build dictionaries/collections outside the precompiled query and capture them","Prefer List<T> when a collection initializer is needed"],"tags":["efcore","precompiled-queries","collection-initializers","csharp"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}