{"record":{"id":"a3d5f991582e420a","repo":"dotnet/efcore","slug":"anonymousobjectcreation-unnamed-initializer-with","errorCode":null,"errorMessage":"AnonymousObjectCreation: unnamed initializer with non-MemberAccess expression: {initializer.Expression}","messagePattern":"AnonymousObjectCreation: unnamed initializer with non-MemberAccess expression: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":187,"sourceCode":"            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(\n                        $\"AnonymousObjectCreation: unnamed initializer with non-MemberAccess expression: {initializer.Expression}\");\n\n            var position = Array.FindIndex(parameters, p => p.Name == name);\n            var parameter = parameters[position];\n            var parameterType = ResolveType(parameter.Type)\n                ?? throw new InvalidOperationException(\n                    \"Could not resolve type symbol for: \" + parameter.Type);\n\n            parameterInfos[position] = new FakeParameterInfo(name, parameterType, position);\n            arguments[position] = Visit(initializer.Expression);\n            memberInfos[position] = anonymousType.GetProperty(parameter.Name)!;\n        }\n\n        return New(\n            new FakeConstructorInfo(anonymousType, parameterInfos),\n            arguments: arguments,\n            memberInfos);\n    }","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L169-L205","documentation":"For anonymous-object initializers without an explicit name (`NameEquals is null`), the translator infers the member name from a `MemberAccessExpressionSyntax` (mirroring the C# rule). If the initializer expression is neither named nor a member access, it throws `InvalidOperationException`. This matches C# projection-initializer rules, so the input would not normally compile — the guard catches malformed/synthesized trees.","triggerScenarios":"Translating an anonymous-object creation whose initializer is a bare expression with no inferable name, e.g. a synthesized tree containing `new { SomeCall() }` rather than `new { x = SomeCall() }` or `new { a.Foo }`.","commonSituations":"Hand-built/synthesized syntax nodes passed to the translator, or a code generator emitting non-projection initializers. Normal compiled C# is rejected by the compiler first.","solutions":["Give the initializer an explicit name: `new { name = expr }`.","Or make the initializer a member access whose name can be inferred: `new { source.Member }`.","If generating syntax trees programmatically, follow C# projection-initializer rules."],"exampleFix":"// before\nnew { ctx.Method() }\n// after\nnew { value = ctx.Method() }","handlingStrategy":"validation","validationCode":"// For hand-built trees: ensure every anonymous initializer is nameable.\nstatic bool IsNameable(AnonymousObjectMemberDeclaratorSyntax init)\n    => init.NameEquals is not null\n       || init.Expression is MemberAccessExpressionSyntax;\n\nif (!anon.Initializers.All(IsNameable))\n    throw new InvalidOperationException(\"Every anonymous initializer must have a name or be a member access.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always provide explicit names for computed anonymous members: `new { name = expr }`.","When synthesizing trees, follow C# projection-initializer rules.","Avoid method-call or literal initializers without an explicit name."],"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"}