{"record":{"id":"162132e096da7d9d","repo":"dotnet/efcore","slug":"async-lambdas-are-not-supported","errorCode":null,"errorMessage":"Async lambdas are not supported","messagePattern":"Async lambdas are not supported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":1042,"sourceCode":"    /// </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        {\n            if (_semanticModel.GetDeclaredSymbol(parameter) is not { } parameterSymbol\n                || ResolveType(parameterSymbol.Type) is not { } parameterType)\n            {\n                throw new InvalidOperationException(\"Could not found symbol for parameter lambda: \" + parameter);\n            }","sourceCodeStart":1024,"sourceCodeEnd":1060,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L1024-L1060","documentation":"Thrown by CSharpToLinqTranslator when a Roslyn lambda node being turned into a LINQ expression tree has the 'async' keyword (lambda.AsyncKeyword is not SyntaxKind.None). Expression trees cannot represent async control flow, so the translator rejects async lambdas outright during the lambda visit.","triggerScenarios":"An EF query passed through CSharpToLinqTranslator contains a lambda marked async, e.g. db.Users.Select(async u => await FetchAsync(u)) reaching VisitLambdaExpression where the AsyncKeyword token is present.","commonSituations":"Mixing async/await into an IQueryable LINQ operator that takes an Expression<Func<...>> (the C# compiler usually blocks this, but the translator parses raw source, so a manual Roslyn-constructed tree or a delegate overload can still reach this path); or a source-generator build of a project that mistakenly authored an async lambda in the query expression.","solutions":["Remove the 'async' keyword from the lambda; do synchronous work or materialize results before awaiting.","Move the async work out of the expression tree: project on the client after ToListAsync/ToArrayAsync rather than inside the IQueryable Select/Where.","If awaiting server-side operations is required, restructure to call EF async APIs (ToListAsync) instead of embedding await inside the lambda."],"exampleFix":"// before\nvar names = await db.Users\n    .Select(async u => await FormatAsync(u.Name))\n    .ToListAsync();\n\n// after\nvar users = await db.Users.ToListAsync();\nvar names = await Task.WhenAll(users.Select(u => FormatAsync(u.Name)));","handlingStrategy":"validation","validationCode":"// Reject async lambdas before translation.\nusing Microsoft.CodeAnalysis.CSharp.Syntax;\nusing Microsoft.CodeAnalysis.CSharp;\n\nbool IsSyncExpressionLambda(AnonymousFunctionExpressionSyntax lambda)\n    => lambda.AsyncKeyword.IsKind(SyntaxKind.None)\n       && lambda.ExpressionBody is not null;\n\nif (!IsSyncExpressionLambda(lambdaNode))\n    throw new InvalidOperationException(\"Async lambdas cannot be translated; do async work outside the query.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use await inside an EF query lambda; materialize first, then await client-side async work.","Add an analyzer that disallows 'async' tokens inside IQueryable LINQ operator arguments.","Prefer EF async APIs (ToListAsync, FirstOrDefaultAsync) over embedding await in the expression."],"tags":["ef-core","query-translation","async","linq-expressions","csharp","precompiled-queries"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}