{"record":{"id":"50e7f181d3b07b3c","repo":"dotnet/efcore","slug":"lambda-with-null-expression-body","errorCode":null,"errorMessage":"Lambda with null expression body","messagePattern":"Lambda with null expression body","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":1032,"sourceCode":"\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    {\n        if (lambda.ExpressionBody is null)\n        {\n            throw new NotSupportedException(\"Lambda with null expression body\");\n        }\n\n        if (lambda.Modifiers.Any())\n        {\n            throw new NotSupportedException(\"Lambda with modifiers not supported: \" + lambda.Modifiers);\n        }\n\n        if (!lambda.AsyncKeyword.IsKind(SyntaxKind.None))\n        {\n            throw new NotSupportedException(\"Async lambdas are not supported\");\n        }\n\n        var lambdaParameters = lambda switch\n        {\n            SimpleLambdaExpressionSyntax simpleLambda => SyntaxFactory.SingletonSeparatedList(simpleLambda.Parameter),\n            ParenthesizedLambdaExpressionSyntax parenthesizedLambda => parenthesizedLambda.ParameterList.Parameters,\n\n            _ => throw new UnreachableException()","sourceCodeStart":1014,"sourceCodeEnd":1050,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L1014-L1050","documentation":"Thrown by CSharpToLinqTranslator.VisitLambdaExpression when a lambda's ExpressionBody is null. The translator only handles expression-bodied lambdas (x => x + 1) because it produces a single LINQ Expression, not a statement tree; statement lambdas with block bodies ((x) => { ... }) are rejected. This is part of EF Core's precompiled-query infrastructure that parses Roslyn C# syntax back into LINQ expression trees.","triggerScenarios":"Translating C# source containing a statement-bodied lambda (braces) passed to a LINQ operator during precompiled query processing; any AnonymousFunctionExpressionSyntax where lambda.ExpressionBody is null.","commonSituations":"Writing a precompiled query predicate with multiple statements inside braces; using method-group or block lambdas in code that gets processed by the C#-to-LINQ translator; malformed syntax trees from a source generator.","solutions":["Rewrite the lambda as an expression-bodied lambda (single expression, no braces).","Extract multi-statement logic into a separate method and reference it as a single expression.","Simplify the query predicate to a single boolean expression."],"exampleFix":"// before\n.Where(x => { if (x.Age < 18) return false; return x.Name.StartsWith(\"A\"); })\n// after\n.Where(x => x.Age >= 18 && x.Name.StartsWith(\"A\"))","handlingStrategy":"validation","validationCode":"// Before translating user query source, scan lambdas for block bodies\nforeach (var lambda in root.DescendantNodes().OfType<AnonymousFunctionExpressionSyntax>())\n{\n    if (lambda.ExpressionBody is null)\n        throw new InvalidOperationException(\n            $\"Statement-bodied lambda at {lambda.GetLocation()} is not supported; use an expression-bodied lambda.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use expression-bodied lambdas (no braces) in all query code intended for precompilation.","Add a source-analyzer/roslyn analyzer rule that flags statement lambdas inside query operator calls."],"tags":["efcore","lambda","expression-trees","precompiled-queries","translator"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}