{"record":{"id":"37bf1be3aa57902c","repo":"dotnet/efcore","slug":"could-not-find-symbol-for-anonymous-object-creatio","errorCode":null,"errorMessage":"Could not find symbol for anonymous object creation initializer: {anonymousObjectCreation}","messagePattern":"Could not find symbol for anonymous object creation initializer: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":169,"sourceCode":"\n        // TODO: Insert necessary Convert nodes etc. when the expected and actual types differ\n\n        return result;\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 VisitAnonymousObjectCreationExpression(AnonymousObjectCreationExpressionSyntax anonymousObjectCreation)\n    {\n        // Creating an actual anonymous object means creating a new type, which can only be done with Reflection.Emit.\n        // At least for EF's purposes, it doesn't matter, so we build a placeholder.\n        if (_semanticModel.GetSymbolInfo(anonymousObjectCreation).Symbol is not IMethodSymbol constructorSymbol)\n        {\n            throw new InvalidOperationException(DesignStrings.NoAnonymousSymbol + \" \" + anonymousObjectCreation);\n        }\n\n        var anonymousType = ResolveType(constructorSymbol.ContainingType);\n\n        var parameters = constructorSymbol.Parameters.ToArray();\n\n        var parameterInfos = new ParameterInfo[parameters.Length];\n        var memberInfos = new MemberInfo[parameters.Length];\n        var arguments = new Expression[parameters.Length];\n\n        foreach (var initializer in anonymousObjectCreation.Initializers)\n        {\n            // If the initializer's name isn't explicitly specified, infer it from the initializer's expression like the compiler does\n            var name = initializer.NameEquals is not null\n                ? initializer.NameEquals.Name.Identifier.Text\n                : initializer.Expression is MemberAccessExpressionSyntax memberAccess\n                    ? memberAccess.Name.Identifier.Text\n                    : throw new InvalidOperationException(","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L151-L187","documentation":"In `VisitAnonymousObjectCreationExpression` the translator does `GetSymbolInfo(anonymousObjectCreation).Symbol` and expects an `IMethodSymbol` (the anonymous-type constructor). If Roslyn returns null it throws `InvalidOperationException(NoAnonymousSymbol + ...)`, meaning the semantic model could not bind the `new { ... }` expression to a constructor.","triggerScenarios":"Translating a precompiled query containing an anonymous-object creation (`new { a.X, b.Y }`) when the `SemanticModel` cannot resolve the anonymous-type constructor — typically because the compilation lacks references or the syntax tree was not added to the compilation that produced the semantic model.","commonSituations":"Building a `SemanticModel` from a `CSharpCompilation` that is missing references (so the anonymous type symbol is unbound), or passing a `SemanticModel` from a different compilation than the one given to `Load`.","solutions":["Ensure the syntax tree is part of the same `Compilation` used to build the `SemanticModel` and passed to `Load`.","Add all metadata references the query needs (entity types, BCL) to the compilation.","If anonymous projection isn't required, project into a named DTO instead."],"exampleFix":"// before\nvar sm = otherCompilation.GetSemanticModel(tree);   // wrong compilation\ntranslator.Translate(node, sm);\n// after\nvar sm = compilation.GetSemanticModel(tree);\ntranslator.Translate(node, sm);","handlingStrategy":"validation","validationCode":"// Confirm the SemanticModel binds the anonymous constructor before translating.\nvar symbol = semanticModel.GetSymbolInfo(anonymousNode).Symbol;\nif (symbol is not IMethodSymbol)\n    throw new InvalidOperationException(\n        \"The anonymous object creation could not be bound. \" +\n        \"Ensure the tree is in the same compilation and references are present.\");\n\ntranslator.Translate(anonymousNode, semanticModel);","typeGuard":null,"tryCatchPattern":"try { translator.Translate(node, semanticModel); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Could not find symbol for anonymous object creation\"))\n{ /* verify tree belongs to compilation, add references, retry */ }","preventionTips":["Build the `SemanticModel` from the same `Compilation` passed to `Load`.","Include all references needed to bind anonymous types (entity + BCL assemblies).","Prefer named DTO projections when binding is fragile."],"tags":["ef-core","precompiled-queries","roslyn","anonymous-types"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}