{"record":{"id":"a209dc52e6c17f2a","repo":"dotnet/efcore","slug":"lambda-with-modifiers-not-supported-lambda-modif","errorCode":null,"errorMessage":"Lambda with modifiers not supported: {lambda.Modifiers}","messagePattern":"Lambda with modifiers not supported: (.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":1037,"sourceCode":"    /// <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()\n        };\n\n        var translatedParameters = new List<ParameterExpression>();\n        foreach (var parameter in lambdaParameters)\n        {","sourceCodeStart":1019,"sourceCodeEnd":1055,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L1019-L1055","documentation":"Thrown by CSharpToLinqTranslator when translating a Roslyn lambda node (AnonymousFunctionExpressionSyntax) into a LINQ expression tree and the lambda carries one or more modifiers (e.g. 'static' from C# 9 static lambdas, or 'ref'/'out'/'in' on parameters). The translator only supports plain expression-bodied lambdas; the modifier surface is intentionally rejected because expression trees cannot represent those semantics.","triggerScenarios":"A query predicate passed to EF's precompiled-query/source-generator pipeline contains a lambda with modifiers, e.g. source.Where(static x => x.Active) or a lambda declared with parameter modifiers that reaches VisitLambdaExpression with a non-empty lambda.Modifiers.","commonSituations":"Authoring EF Core queries in a project that opts into the precompiled-queries source generator and using C# 9+ 'static' lambdas (often for performance/escape semantics) inside the query; or copying LINQ from code that used static lambdas for delegate caching.","solutions":["Remove the 'static' (or other) modifier from the lambda used in the EF query so it is a plain expression lambda.","If the modifier is required for other call sites, extract the lambda body into a separate non-modified method or local function and pass that to the query.","Move the query off the precompiled-query/source-generator pipeline for that specific call (e.g. use a normal compiled query) until EF Core adds support for the modifier."],"exampleFix":"// before\nvar users = db.Users.Where(static u => u.IsActive).ToList();\n\n// after\nvar users = db.Users.Where(u => u.IsActive).ToList();","handlingStrategy":"validation","validationCode":"// Before handing a lambda node to CSharpToLinqTranslator.Translate,\n// verify it carries no modifiers.\nbool IsSafeLambda(Microsoft.CodeAnalysis.CSharp.Syntax.AnonymousFunctionExpressionSyntax lambda)\n    => !lambda.Modifiers.Any()\n       && lambda.AsyncKeyword.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.None)\n       && lambda.ExpressionBody is not null;\n\nif (!IsSafeLambda(lambdaNode)) throw new InvalidOperationException(\"Lambda uses unsupported modifiers; rewrite as a plain expression lambda.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not author 'static' lambdas inside EF query expressions when the project uses the precompiled-queries source generator.","Run a Roslyn analyzer / source-generator diagnostic rule that flags lambda modifiers in query operator arguments.","Keep query lambdas as plain expression lambdas; reserve static lambdas for non-query call sites."],"tags":["ef-core","query-translation","linq-expressions","csharp","precompiled-queries","roslyn"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}