Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9162639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:15:30+00:00 2026-06-17T14:15:30+00:00

In the simple snippet below: static void Main(string[] args) { Expression<Func<string, bool>> equal =

  • 0

In the simple 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();
}

The compile method fails with an InvalidOperationException

“variable ‘s’ of type ‘System.String’ referenced from scope ”,
but it is not defined”.

While I’ve found other similar questions, I’ve failed to understand the answers provided.

The DebugView of the LambdaExpression contains this:

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

So where I’ve seen in other examples of this question, the parameter and the usage don’t match up, this doesn’t appear to be an issue here. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:15:31+00:00Added an answer on June 17, 2026 at 2:15 pm

    Parameters of lambda expressions don’t use equality based on names, they use reference equality. So, in your expression, you have two entirely different parameters called s, one is defined but not used, the other is used but not defined.

    So, basically, your expression is something like s1 => s2 == "test", which is why it fails to compile. The simplest fix would be to use the same parameter the original expression uses:

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

    If you really wanted to use your own parameter, you would need to replace all instances of the original parameter in the lambda body with your new parameter. Probably the best way to do that is to use ExpressionVisitor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble with some javascript. The code snippet below creates a simple
I am using the below code snippet to FTP simple text files from a
I am trying to understand the recursion call in the below code snippet. static
The code snippet below displays a Dialog with a simple login-form. The problem is
I've got a very simple snippet of HTML (below) - and both sections seem
For the simple snippet below a = raw_input(Enter a number: ) How can I
Below is the simple xml snippet for which android:textStyle=italic is not applying. If I
In my little code snippet below I have a wrapper class for a simple
Below is a simple code snippet that demonstrates the seemingly buggy behavior of end
The following snippet uses simple Java code. package pkg; final public class Main {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.