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 8941689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:21:47+00:00 2026-06-15T11:21:47+00:00

I need to create a dynamic linq expression an i started work with many

  • 0

I need to create a dynamic linq expression an i started work with many examples. I tested some and some work and some not. In this case i want to create a method that looks like :

public bool Check(int intvar)
{
   if ( i > 2 )
     return true;
   else
     return false;
}

Now i have written the following :

LabelTarget returnTarget = Expression.Label("label");
ParameterExpression para = Expression.Parameter(typeof(int), "intvalue");
Expression test = Expression.GreaterThan(para, Expression.Constant(5));
Expression iftrue = Expression.Return(returnTarget, Expression.Constant(true));
Expression iffalse = Expression.Return(returnTarget,                   Expression.Constant(false));
Expression.IfThenElse(test, iftrue, iffalse);

this.TheExpression = Expression.IfThenElse(test, iftrue, iffalse);
Expression.Lambda<Action<int>>(
this.TheExpression,
new ParameterExpression[] { para }
).Compile()(5);

Now it throws InvalidOperationException:

Cannot jump to the label “label”`

What is wrong ? I only need a return true or false.

  • 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-15T11:21:48+00:00Added an answer on June 15, 2026 at 11:21 am

    You need to change a few things:

    • Put the return label at the bottom of your function in a block expression, as René suggested. This is where your return statement will jump.

    • Declare the Lambda as type Func<int, bool>. Since you want a return value, this needs to be a function, not an action.

    • Declare the returnTarget label as type bool. Since the return value of a block expression is the value of its last statement, the label must be of the correct type.

    • Provide a default value for the final label (= the return value of your function if the label is reached by normal control flow instead of a return statement).

      LabelTarget returnTarget = Expression.Label(typeof(bool));
      ParameterExpression para = Expression.Parameter(typeof(int), "intvalue");
      Expression test = Expression.GreaterThan(para, Expression.Constant(5));
      Expression iftrue = Expression.Return(returnTarget, Expression.Constant(true));
      Expression iffalse = Expression.Return(returnTarget, Expression.Constant(false));
      
      var ex = Expression.Block(
          Expression.IfThenElse(test, iftrue, iffalse),
          Expression.Label(returnTarget, Expression.Constant(false)));
      
      var compiled = Expression.Lambda<Func<int, bool>>(
          ex,
          new ParameterExpression[] { para }
      ).Compile();
      
      Console.WriteLine(compiled(5));     // prints "False"
      Console.WriteLine(compiled(6));     // prints "True"
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create some dynamic menus in a VS2010 SDI application I'm writing.
i want to write head tag. i need create dynamic <head id=htmlHead runat=server> </head>
I need to create dynamic INNER JOIN (I believe I need to anyway). I
I need to create a dynamic array in Java, but the values type differ
I need to create a real dynamic JPA CriteriaBuilder . I get an Map<String,
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
I need to create dynamic radio button in my table.I have a table in
We need to create dynamic WHERE Clauses depending on user input. Using the old
I need to create dynamic 'Pay Now' buttons on my site, and PayPal says

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.