{"record":{"id":"1deabcf0d9743da9","repo":"dotnet/efcore","slug":"listinit-lifted-statements","errorCode":null,"errorMessage":"ListInit: lifted statements","messagePattern":"ListInit: lifted statements","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":2597,"sourceCode":"            // C# collection initialization syntax only works when Add is called on an IEnumerable, but LINQ supports arbitrary add\n            // methods. Skip these, we'll add them later outside the initializer\n            if (!listInit.NewExpression.Type.IsAssignableTo(typeof(IEnumerable))\n                || listInit.Initializers.Any(e => e.AddMethod.Name != \"Add\" || e.Arguments.Count != 1))\n            {\n                incompatibleListBindings ??= [];\n                incompatibleListBindings.Add(initializer);\n                continue;\n            }\n\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    }","sourceCodeStart":2579,"sourceCodeEnd":2615,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L2579-L2615","documentation":"Symmetric to the MemberInit lifted-statements case but for ListInitExpression: if translating a collection-initializer element produces lifted statements, the translator cannot position those statements after the instantiation (it normally lifts to before), so it throws NotImplementedException(\"ListInit: lifted statements\").","triggerScenarios":"A collection initializer element expression with side effects requiring lifting, e.g. new List<T> { SomeMethod() } where the element needs hoisting.","commonSituations":"Collection initializers in precompiled queries where elements involve method calls or other side-effecting expressions.","solutions":["Compute element values into locals first, then add them.","Use side-effect-free element expressions (constants, parameter reads).","Construct the collection and call Add explicitly instead of using an initializer."],"exampleFix":"// before\nvar li = Expression.ListInit(newList, Expression.ElementInit(add, MethodCall()));\n// after\n// compute tmp = MethodCall() first, then list init with tmp","handlingStrategy":"validation","validationCode":"// Flag ListInit elements that have side effects\nprotected override Expression VisitListInit(ListInitExpression li) {\n    if (li.Initializers.Any(e => e.Arguments.Any(a => new SideEffectDetector().HasSideEffects(a)))) Found = true;\n    return li;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Precompute element values into locals before collection initialization.","Keep initializer elements side-effect-free.","Construct the collection and call Add explicitly."],"tags":["ef-core","linq","expression-tree","collections","object-init"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}