dotnet/efcore · error · InvalidOperationException
No type for expression {elementAccessExpression.Expression}
Error message
No type for expression {elementAccessExpression.Expression} in ElementAccessExpressionSyntax What it means
`VisitElementAccessExpression` switches on `GetTypeInfo(elementAccessExpression.Expression).ConvertedType`; the `case null:` arm throws `InvalidOperationException` when the semantic model cannot determine the type of the expression being indexed (`obj[...]` where `obj` has no resolvable type).
Source
Thrown at src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:384
return ArrayIndex(visitedExpression, Visit(arguments[0].Expression));
case INamedTypeSymbol:
var property = visitedExpression.Type
.GetProperties()
.Select(p => new { Property = p, IndexParameters = p.GetIndexParameters() })
.Where(t => t.IndexParameters.Length == arguments.Count
&& t.IndexParameters
.Select(p => p.ParameterType)
.SequenceEqual(arguments.Select(a => ResolveType(a.Expression))))
.Select(t => t.Property)
.FirstOrDefault();
Check.DebugAssert(property?.GetMethod is not null, "No matching property found for ElementAccessExpressionSyntax");
return Call(visitedExpression, property.GetMethod, arguments.Select(a => Visit(a.Expression)));
case null:
throw new InvalidOperationException(
$"No type for expression {elementAccessExpression.Expression} in {nameof(ElementAccessExpressionSyntax)}");
default:
throw new NotImplementedException($"{nameof(ElementAccessExpressionSyntax)} over non-array");
}
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override Expression VisitIdentifierName(IdentifierNameSyntax identifierName)
{
if (_parameterStack.Peek().TryGetValue(identifierName.Identifier.Text, out var parameter))
{
return parameter;View on GitHub (pinned to dbf9771522)
Solutions
- Ensure the receiver's type assembly is referenced / passed as `additionalAssembly`.
- Make sure the tree belongs to the compilation that built the `SemanticModel`.
- Replace the indexer with an equivalent method call that resolves cleanly.
Example fix
// before
translator.Translate(indexNode, smWithMissingRefs);
// after
var compilation = baseCompilation
.AddReferences(MetadataReference.CreateFromFile(typeof(ReceiverType).Assembly.Location));
translator.Translate(indexNode, compilation.GetSemanticModel(tree)); Defensive patterns
Strategy: validation
Validate before calling
// Ensure the indexer receiver has a resolvable converted type.
var converted = semanticModel.GetTypeInfo(elementAccess.Expression).ConvertedType;
if (converted is null)
throw new InvalidOperationException(
"The indexer receiver has no resolvable type. Add the needed references and ensure the tree is in the compilation.");
translator.Translate(elementAccess, semanticModel); Try / catch
try { translator.Translate(node, semanticModel); }
catch (InvalidOperationException ex) when (ex.Message.Contains("No type for expression") && ex.Message.Contains("ElementAccessExpressionSyntax"))
{ /* add references / fix tree membership, then retry */ } Prevention
- Reference the assembly that defines the indexer receiver's type.
- Keep the tree inside the compilation that builds the `SemanticModel`.
- Avoid indexers over types from unloaded assemblies.
When it happens
Trigger: Translating an indexer expression `a[i]` where the type of `a` is unbound — typically because the `SemanticModel` is incomplete or the receiver's type is in an unreferenced assembly.
Common situations: Missing metadata references, or using a `SemanticModel` from a compilation that does not contain the receiver's type definition.
Related errors
- Could not resolve type symbol for: {parameter.Type}
- ArrayCreation: non-array type symbol: {arrayCreation}
- Identifier without symbol: {identifierName}
- Could not find type symbol for: {fullyQualifiedMetadataName}
- A compilation must be loaded.
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/b247ac527be6c727.
Report an issue: GitHub.