dotnet/efcore · error · NotSupportedException

Lambda with modifiers not supported

Error message

Lambda with modifiers not supported: {lambda.Modifiers}

What it means

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.

Solutions

  1. Remove the 'static' (or other) modifier from the lambda used in the EF query so it is a plain expression lambda.
  2. 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.
  3. 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.

Example fix

// before
var users = db.Users.Where(static u => u.IsActive).ToList();

// after
var users = db.Users.Where(u => u.IsActive).ToList();
Defensive patterns

Strategy: validation

Validate before calling

// Before handing a lambda node to CSharpToLinqTranslator.Translate,
// verify it carries no modifiers.
bool IsSafeLambda(Microsoft.CodeAnalysis.CSharp.Syntax.AnonymousFunctionExpressionSyntax lambda)
    => !lambda.Modifiers.Any()
       && lambda.AsyncKeyword.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.None)
       && lambda.ExpressionBody is not null;

if (!IsSafeLambda(lambdaNode)) throw new InvalidOperationException("Lambda uses unsupported modifiers; rewrite as a plain expression lambda.");

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/a209dc52e6c17f2a. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:1037

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override Expression DefaultVisit(SyntaxNode node)
        => throw new NotSupportedException($"Unsupported syntax node of type '{node.GetType()}': {node}");

    private Expression VisitLambdaExpression(AnonymousFunctionExpressionSyntax lambda, Type? expectedType = null)
    {
        if (lambda.ExpressionBody is null)
        {
            throw new NotSupportedException("Lambda with null expression body");
        }

        if (lambda.Modifiers.Any())
        {
            throw new NotSupportedException("Lambda with modifiers not supported: " + lambda.Modifiers);
        }

        if (!lambda.AsyncKeyword.IsKind(SyntaxKind.None))
        {
            throw new NotSupportedException("Async lambdas are not supported");
        }

        var lambdaParameters = lambda switch
        {
            SimpleLambdaExpressionSyntax simpleLambda => SyntaxFactory.SingletonSeparatedList(simpleLambda.Parameter),
            ParenthesizedLambdaExpressionSyntax parenthesizedLambda => parenthesizedLambda.ParameterList.Parameters,

            _ => throw new UnreachableException()
        };

        var translatedParameters = new List<ParameterExpression>();
        foreach (var parameter in lambdaParameters)
        {

View on GitHub (pinned to 3a2006ef56)