{"record":{"id":"7c9f6933c4c16abc","repo":"dotnet/efcore","slug":"unable-to-translate-type-type","errorCode":null,"errorMessage":"Unable to translate type '{type}'.","messagePattern":"Unable to translate type '(.+?)'\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":1314,"sourceCode":"        if (labelTarget.Type != typeof(void))\n        {\n            throw new NotImplementedException(\"Non-void label target\");\n        }\n\n        // We did a processing pass on the block's labels, so any labels should already be found in our label stack frame\n        return IdentifierName(_stack.Peek().Labels[labelTarget]);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual TypeSyntax Generate(Type type)\n        => TryGenerate(type, out var result)\n            ? result\n            : throw new NotSupportedException(DesignStrings.UnableToTranslateType(type.DisplayName(fullName: false)));\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual bool TryGenerate(Type type, [NotNullWhen(true)] out TypeSyntax? result)\n    {\n        result = null;\n        if (type.IsAnonymousType())\n        {\n            return false;\n        }\n\n        if (type.IsGenericType)\n        {\n            if (type.IsConstructedGenericType","sourceCodeStart":1296,"sourceCodeEnd":1332,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L1296-L1332","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before — anonymous type used as a generic argument (cannot be named)\nvar anon = db.Users.Select(u => new { u.Id, u.Name }).First();\n// some downstream expression references the anonymous type explicitly\n// e.g. Expression.Call(typeof(SomeHelper), \"Do\", new[] { anon.GetType() }, ...)\n\n// after — use a named DTO so Generate(Type) succeeds\nclass UserDto { public int Id { get; set; } public string Name { get; set; } }\nvar dto = db.Users.Select(u => new UserDto { Id = u.Id, Name = u.Name }).First();","handlingStrategy":"validation","validationCode":"// Verify every Type the translator will be asked to name is generatable.\nusing System.Linq.Expressions;\n\nbool HasUnrepresentableType(Expression e)\n{\n    bool bad = false;\n    new TypeChecker(t =>\n    {\n        if (t.IsAnonymousType()) bad = true;\n        if (t.IsGenericType)\n            foreach (var arg in t.GetGenericArguments())\n                if (arg.IsAnonymousType()) bad = true;\n    }).Visit(e);\n    return bad;\n}\n\nclass TypeChecker(Action<Type> onType) : ExpressionVisitor\n{\n    protected override Expression VisitConstant(ConstantExpression c)\n    { if (c.Value is Type t) onType(t); return base.VisitConstant(c); }\n}","typeGuard":"static bool IsAnonymous(Type t)\n    => t is not null\n       && t.Name.Contains(\"AnonymousType\")\n       && Attribute.IsDefined(t, typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute));","tryCatchPattern":null,"preventionTips":["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."],"tags":["ef-core","expression-trees","anonymous-types","generics","code-generation","type-resolution"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}