dotnet/efcore · error · NotImplementedException
ListInit: incompatible ElementInit
Error message
ListInit: incompatible ElementInit
What it means
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").
Source
Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:2606
var liftedStatementsPosition = _liftedState.Statements.Count;
VisitElementInit(initializer);
initializerExpressions.Add((ExpressionSyntax)Result!);
if (_liftedState.Statements.Count > liftedStatementsPosition)
{
throw new NotImplementedException("ListInit: lifted statements");
}
}
if (incompatibleListBindings is not null)
{
// TODO: This requires lifting statements to *after* the instantiation - we usually lift to before.
// This is problematic: if such an expression is passed as an argument to a method, there's no way to faithfully translate it
// while preserving evaluation order.
throw new NotImplementedException("ListInit: incompatible ElementInit");
}
Result = objectCreation.WithInitializer(
InitializerExpression(
SyntaxKind.CollectionInitializerExpression,
SeparatedList(initializerExpressions)));
return listInit;
}
/// <inheritdoc />
protected override ElementInit VisitElementInit(ElementInit elementInit)
{
Check.DebugAssert(elementInit.Arguments.Count == 1);
Visit(elementInit.Arguments.Single());
return elementInit;View on GitHub (pinned to dbf9771522)
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.
Example fix
// before
new Dictionary<int,string> { { 1, "a" } } // Add(int,string) -> 2 args -> throws
// after
var d = new Dictionary<int,string>(); d.Add(1, "a"); // explicit adds after construction Defensive patterns
Strategy: validation
Validate before calling
// Flag ListInit whose type is not IEnumerable-with-single-arg-Add
protected override Expression VisitListInit(ListInitExpression li) {
if (!typeof(IEnumerable).IsAssignableFrom(li.NewExpression.Type)
|| li.Initializers.Any(e => e.AddMethod.Name != "Add" || e.Arguments.Count != 1)) Found = true;
return li;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Dictionary initializers using Add(k,v); custom collections with multi-argument Add used inside compiled query expressions.
Related errors
- MemberInit: incompatible MemberListBinding
- ListInit: lifted statements
- MemberInit: lifted statements
- DebugInfo nodes are not supporting when translating expressi
- Null argument in VisitLabelTarget
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/c3a954db51543f75.
Report an issue: GitHub.