{"record":{"id":"2e5909bc7aa91d67","repo":"dotnet/efcore","slug":"could-not-find-symbol-for-typeof-expression-ty","errorCode":null,"errorMessage":"Could not find symbol for typeof() expression: {typeOf}","messagePattern":"Could not find symbol for typeof\\(\\) expression: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":1011,"sourceCode":"    ///     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    public override Expression VisitSimpleLambdaExpression(SimpleLambdaExpressionSyntax lambda)\n        => VisitLambdaExpression(lambda);\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    public override Expression VisitTypeOfExpression(TypeOfExpressionSyntax typeOf)\n    {\n        if (_semanticModel.GetSymbolInfo(typeOf.Type).Symbol is not ITypeSymbol typeSymbol)\n        {\n            throw new InvalidOperationException(\n                \"Could not find symbol for typeof() expression: \" + typeOf);\n        }\n\n        var type = ResolveType(typeSymbol);\n        return Constant(type, typeof(Type));\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    public override Expression DefaultVisit(SyntaxNode node)\n        => throw new NotSupportedException($\"Unsupported syntax node of type '{node.GetType()}': {node}\");\n\n    private Expression VisitLambdaExpression(AnonymousFunctionExpressionSyntax lambda, Type? expectedType = null)\n    {","sourceCodeStart":993,"sourceCodeEnd":1029,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L993-L1029","documentation":"VisitTypeOfExpression throws InvalidOperationException when typeof(T) inside a precompiled query cannot be resolved to an ITypeSymbol by Roslyn. The translator needs the type symbol to map typeof to a Constant expression over System.Type.","triggerScenarios":"typeof(SomeType) where SomeType is in an unreferenced assembly, is an error type, or otherwise fails to bind in the precompilation.","commonSituations":"typeof() of a type defined in an assembly not referenced by the precompiled-query compilation; typeof of a type whose source has compile errors.","solutions":["Reference the assembly that defines the type used in typeof()","Avoid typeof() of unresolved types in precompiled queries","Capture the needed Type in a local variable outside the query and reference that"],"exampleFix":"// before (ExternalFlag in unreferenced assembly)\nvar q = ctx.Items.Where(i => i.Kind == typeof(ExternalFlag));\n// after (referenced type)\nvar q = ctx.Items.Where(i => i.Kind == typeof(KnownFlag));","handlingStrategy":"validation","validationCode":"// Ensure the type used in typeof() is referenced by the precompilation.\nvar t = typeof(ExternalFlag);\n// If the type is in another assembly, add a MetadataReference for that assembly.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reference the assembly of any type used in typeof() within a precompiled query","Avoid typeof() of error/unresolved types","Capture the needed Type in a local computed outside the query"],"tags":["efcore","precompiled-queries","roslyn","typeof","csharp"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}