{"record":{"id":"c3a954db51543f75","repo":"dotnet/efcore","slug":"listinit-incompatible-elementinit","errorCode":null,"errorMessage":"ListInit: incompatible ElementInit","messagePattern":"ListInit: incompatible ElementInit","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":2606,"sourceCode":"\n            var liftedStatementsPosition = _liftedState.Statements.Count;\n\n            VisitElementInit(initializer);\n\n            initializerExpressions.Add((ExpressionSyntax)Result!);\n\n            if (_liftedState.Statements.Count > liftedStatementsPosition)\n            {\n                throw new NotImplementedException(\"ListInit: lifted statements\");\n            }\n        }\n\n        if (incompatibleListBindings is not null)\n        {\n            // TODO: This requires lifting statements to *after* the instantiation - we usually lift to before.\n            // This is problematic: if such an expression is passed as an argument to a method, there's no way to faithfully translate it\n            // while preserving evaluation order.\n            throw new NotImplementedException(\"ListInit: incompatible ElementInit\");\n        }\n\n        Result = objectCreation.WithInitializer(\n            InitializerExpression(\n                SyntaxKind.CollectionInitializerExpression,\n                SeparatedList(initializerExpressions)));\n\n        return listInit;\n    }\n\n    /// <inheritdoc />\n    protected override ElementInit VisitElementInit(ElementInit elementInit)\n    {\n        Check.DebugAssert(elementInit.Arguments.Count == 1);\n\n        Visit(elementInit.Arguments.Single());\n\n        return elementInit;","sourceCodeStart":2588,"sourceCodeEnd":2624,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L2588-L2624","documentation":"C# collection initializers need a single-argument Add on an IEnumerable. If any ElementInit.AddMethod is not named Add or has other than one argument, or the constructed type is not IEnumerable, the translator cannot lift the calls and throws NotImplementedException(\"ListInit: incompatible ElementInit\").","triggerScenarios":"A ListInitExpression whose initializer uses a custom multi-argument Add method (e.g. dictionary Add(key,value)) or a non-Add method, or whose type is not IEnumerable.","commonSituations":"Dictionary initializers using Add(k,v); custom collections with multi-argument Add used inside compiled query expressions.","solutions":["For dictionaries or multi-arg Add collections, add entries after construction instead of via an initializer.","Use single-argument Add collections (List<T>) for initializer syntax in compiled queries.","Construct the collection and call Add explicitly."],"exampleFix":"// before\nnew Dictionary<int,string> { { 1, \"a\" } }  // Add(int,string) -> 2 args -> throws\n// after\nvar d = new Dictionary<int,string>(); d.Add(1, \"a\"); // explicit adds after construction","handlingStrategy":"validation","validationCode":"// Flag ListInit whose type is not IEnumerable-with-single-arg-Add\nprotected override Expression VisitListInit(ListInitExpression li) {\n    if (!typeof(IEnumerable).IsAssignableFrom(li.NewExpression.Type)\n        || li.Initializers.Any(e => e.AddMethod.Name != \"Add\" || e.Arguments.Count != 1)) Found = true;\n    return li;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use single-argument Add collections for initializer syntax.","Add dictionary entries via explicit Add calls after construction.","Avoid non-Add or multi-argument Add methods in collection initializers."],"tags":["ef-core","linq","expression-tree","collections","object-init"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}