Lambda compilation failed, link defined in '' area, but not defined

In the following snippet below:

static void Main(string[] args)
{
    Expression<Func<string, bool>> equal = s => s == "test";
    LambdaExpression lambda = Expression.Lambda(equal.Body, Expression.Parameter(typeof(string), "s"));
    lambda.Compile();
}

Compiled method with InvalidOperationException

"variable 's' of type 'System.String' referring to scope '', but it is not defined."

While I found other similar questions, I could not understand the answers.

The LugdaExpression expression in DebugView contains the following:

.Lambda #Lambda1<System.Func`2[System.String,System.Boolean]>(System.String $s) {
    $s == "test"
}

So, where I saw in other examples of this question, the parameter and usage do not match, this does not seem to be a problem. What am I doing wrong?

+5
source share
1 answer

- , , . , s, , , , .

, , s1 => s2 == "test", . , :

LambdaExpression lambda = Expression.Lambda(equal.Body, equal.Parameters.Single());

, - . , - ExpressionVisitor.

+8

All Articles