dotnet/efcore · error · NotSupportedException
Unable to translate type
Error message
Unable to translate type '{type}'. What it means
Thrown by LinqToCSharpSyntaxTranslator.Generate(Type) when TryGenerate(type, out result) returns false. TryGenerate fails for anonymous types (they cannot be named in C#) and for any generic type whose type arguments cannot themselves be generated, so the type cannot be rendered as a TypeSyntax.
Solutions
- Avoid putting anonymous types into positions where the translator must name them explicitly (use 'var' positions only, where the translator does not need a TypeSyntax).
- Replace anonymous types with a named DTO when the type must appear as a generic argument or cast target.
- Inspect type.IsAnonymousType() before translation; if true, refactor the surrounding tree so the anonymous type is only in inference-friendly positions.
- For internal types, expose a public proxy type the translator can name.
Example fix
// before — anonymous type used as a generic argument (cannot be named)
var anon = db.Users.Select(u => new { u.Id, u.Name }).First();
// some downstream expression references the anonymous type explicitly
// e.g. Expression.Call(typeof(SomeHelper), "Do", new[] { anon.GetType() }, ...)
// after — use a named DTO so Generate(Type) succeeds
class UserDto { public int Id { get; set; } public string Name { get; set; } }
var dto = db.Users.Select(u => new UserDto { Id = u.Id, Name = u.Name }).First(); Defensive patterns
Strategy: validation
Validate before calling
// Verify every Type the translator will be asked to name is generatable.
using System.Linq.Expressions;
bool HasUnrepresentableType(Expression e)
{
bool bad = false;
new TypeChecker(t =>
{
if (t.IsAnonymousType()) bad = true;
if (t.IsGenericType)
foreach (var arg in t.GetGenericArguments())
if (arg.IsAnonymousType()) bad = true;
}).Visit(e);
return bad;
}
class TypeChecker(Action<Type> onType) : ExpressionVisitor
{
protected override Expression VisitConstant(ConstantExpression c)
{ if (c.Value is Type t) onType(t); return base.VisitConstant(c); }
} Type guard
static bool IsAnonymous(Type t)
=> t is not null
&& t.Name.Contains("AnonymousType")
&& Attribute.IsDefined(t, typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)); Prevention
- Keep anonymous types in 'var' inference positions only; never use them as generic arguments or cast targets.
- Use named DTOs where the type must be named explicitly.
- Pre-scan the tree for anonymous-type Type tokens before translation.
When it happens
Trigger: The translator is asked to render a TypeSyntax for a type it cannot name — most commonly an anonymous type appearing in a position that requires an explicit type token (a cast, a variable declaration, a generic argument), or a generic type one of whose arguments is itself an anonymous/unrepresentable type.
Common situations: Anonymous-type values leaking into positions where the translator must emit a concrete type name (e.g. as a generic type argument, which C# forbids for anonymous types); provider extension trees that surface an internal/private type as a Type token; ref-typed generic args that fail TryGenerate recursively.
Related errors
- Anonymous type creation without members
- BinaryExpression with
- Can't translate method
- Compound assignment of private property not yet supported
- DebugInfo nodes are not supporting when translating…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/7c9f6933c4c16abc.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:1314
if (labelTarget.Type != typeof(void))
{
throw new NotImplementedException("Non-void label target");
}
// We did a processing pass on the block's labels, so any labels should already be found in our label stack frame
return IdentifierName(_stack.Peek().Labels[labelTarget]);
}
/// <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>
protected virtual TypeSyntax Generate(Type type)
=> TryGenerate(type, out var result)
? result
: throw new NotSupportedException(DesignStrings.UnableToTranslateType(type.DisplayName(fullName: false)));
/// <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>
protected virtual bool TryGenerate(Type type, [NotNullWhen(true)] out TypeSyntax? result)
{
result = null;
if (type.IsAnonymousType())
{
return false;
}
if (type.IsGenericType)
{
if (type.IsConstructedGenericTypeView on GitHub (pinned to 3a2006ef56)