{"record":{"id":"d390a66862ed4158","repo":"dotnet/efcore","slug":"anonymous-type-creation-without-members","errorCode":null,"errorMessage":"Anonymous type creation without members","messagePattern":"Anonymous type creation without members","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":2110,"sourceCode":"        Result = _g.ArrayCreationExpression(elementType, expressions);\n\n        return newArray;\n    }\n\n    /// <inheritdoc />\n    protected override Expression VisitNew(NewExpression node)\n    {\n        using var _ = ChangeContext(ExpressionContext.Expression);\n\n        var arguments = node.Constructor is null\n            ? []\n            : TranslateMethodArguments(node.Constructor.GetParameters(), node.Arguments);\n\n        if (node.Type.IsAnonymousType())\n        {\n            if (node.Members is null)\n            {\n                throw new NotSupportedException(\"Anonymous type creation without members\");\n            }\n\n            Result = AnonymousObjectCreationExpression(\n                SeparatedList(\n                    arguments.Select((arg, i) =>\n                            AnonymousObjectMemberDeclarator(NameEquals(node.Members[i].Name), arg.Expression))\n                        .ToArray()));\n\n            return node;\n        }\n\n        // If the constructor isn't public, or it has required properties and the constructor doesn't have [SetsRequiredMembers], we can't\n        // just generate a regular instantiation expression (won't compile). Generate an unsafe accessor instead.\n        if (node.Constructor is { } constructor\n            && (!constructor.IsPublic\n                || (node.Type.GetCustomAttribute<RequiredMemberAttribute>() is not null\n                    && constructor.GetCustomAttribute<SetsRequiredMembersAttribute>() is null)))\n        {","sourceCodeStart":2092,"sourceCodeEnd":2128,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L2092-L2128","documentation":"For anonymous-type instantiation the translator maps constructor arguments to property names using node.Members to render new { A = x, B = y }. If node.Members is null it has no names to emit, so VisitNew throws NotSupportedException. The C# compiler always supplies Members for anonymous news; a null Members only arises from low-level Expression.New overloads that omit them.","triggerScenarios":"A NewExpression whose Type.IsAnonymousType() is true but Members is null — produced by Expression.New(constructor, arguments) without the members overload, or by an expression rewriter that drops the Members collection.","commonSituations":"Programmatic construction of anonymous-type news via the lower-level Expression.New; tree rewriting that loses MemberInfo bindings.","solutions":["Use the Expression.New overload that supplies MemberInfo[] members for anonymous types (matching the constructor parameters to properties).","Let the C# compiler build the anonymous new { ... } lambda instead of constructing it by hand.","Use a named type so Members is irrelevant."],"exampleFix":"// before\nvar newExpr = Expression.New(anonCtor, args); // Members is null -> throws\n// after\nvar newExpr = Expression.New(anonCtor, args, anonProperties);","handlingStrategy":"validation","validationCode":"protected override Expression VisitNew(NewExpression n) {\n    if (n.Type.IsAnonymousType() && n.Members is null) Found = true;\n    return n;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the Expression.New overload that supplies MemberInfo[] for anonymous types.","Let the compiler build anonymous news rather than constructing them by hand.","Prefer named types in programmatically built trees."],"tags":["ef-core","linq","expression-tree","anonymous-types","object-creation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}