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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:38:32+00:00 2026-05-16T15:38:32+00:00

I am trying to build a dynamic Expression Tree like below : Func<IEnumerable<int>, int,

  • 0

I am trying to build a dynamic Expression Tree like below :

 Func<IEnumerable<int>, int, bool> dividesectionmethod = (x, y) =>
            {
                int nos1 = 0;
                int nos2 = 0;
                foreach (int i in x)
                {
                    if (i <= y)
                        nos1++;
                    else
                        nos2++;
                }
                return nos1 > nos2;
            };

For that I am using :

 ParameterExpression enumerableExpression = Expression.Parameter(typeof(IEnumerable<int>), "x");
            ParameterExpression intexpression = Expression.Parameter(typeof(int), "y");

            ParameterExpression localvarnos1 = Expression.Variable(typeof(int), "nos1");
            ParameterExpression localvarnos2 = Expression.Variable(typeof(int), "nos2");
            ConstantExpression zeroConstantintval = Expression.Constant(0);
            BinaryExpression bexplocalnos1 = Expression.Assign(localvarnos1, zeroConstantintval);
            BinaryExpression bexplocalnos2 = Expression.Assign(localvarnos2, zeroConstantintval);

            //As Expression does not support Foreach we need to get Enumerator before doing loop

            ParameterExpression enumerator = Expression.Variable(typeof(IEnumerator<int>), "enumerator");
            BinaryExpression assignenumerator = Expression.Assign(enumerator, Expression.Call(enumerableExpression, typeof(IEnumerable<int>).GetMethod("GetEnumerator")));


            var currentelement = Expression.Parameter(typeof(int), "i");
            var callCurrent = Expression.Assign(currentelement, Expression.Property(enumerator, "Current"));

            BinaryExpression firstlessequalsecond = Expression.LessThanOrEqual(currentelement, intexpression);

            MethodCallExpression movenext = Expression.Call(enumerator, typeof(IEnumerator).GetMethod("MoveNext"));

            LabelTarget looplabel = Expression.Label("looplabel");
            LabelTarget returnLabel = Expression.Label(typeof(bool), "retval");

            BlockExpression block = Expression.Block(enumerableExpression, intexpression, localvarnos1, localvarnos2,
                bexplocalnos1, bexplocalnos2, Expression.Loop(Expression.IfThenElse(
                Expression.NotEqual(movenext, Expression.Constant(false)),
                Expression.IfThenElse(firstlessequalsecond, Expression.Increment(localvarnos1), Expression.Increment(localvarnos2)),Expression.Break(looplabel)), looplabel),
                Expression.Return(returnLabel, Expression.LessThan(localvarnos1, localvarnos2)));

            Expression<Func<IEnumerable<int>, int, bool>> lambda = Expression.Lambda<Func<IEnumerable<int>, int, bool>>(block, Expression.Parameter(typeof(IEnumerable<int>), "x"),
                Expression.Parameter(typeof(int), "y"));


            Func<IEnumerable<int>, int, bool> mymethod = lambda.Compile();

But the problem is Expression.Lambda throws an exception :

Expression of type 'System.Void' cannot be used for return type 'System.Boolean'

I dont know the issue as my block seems to be Ok:

.Block() {
    $x;
    $y;
    $nos1;
    $nos2;
    $nos1 = 0;
    $nos2 = 0;
    .Loop  {
        .If (.Call $enumerator.MoveNext() != False) {
            .If ($i <= $y) {
                .Increment($nos1)
            } .Else {
                .Increment($nos2)
            }
        } .Else {
            .Break looplabel { }
        }
    }
    .LabelTarget looplabel:;
    .Return retval { $nos1 < $nos2 }
}

Please let me know what the problem could be.

  • 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-05-16T15:38:33+00:00Added an answer on May 16, 2026 at 3:38 pm

    The value of a BlockExpression is just the value of the last expression in the block. Rather than including a ReturnExpression, just let the last expression be the value you want to return.

    Also, you need to declare the ParameterExpressions for the block as a separate argument to the Expression.Block method. You have included them in the list of expressions, which will cause them to be evaluated as expressions but will not declare them in the block.

    Also, Expression.Increment “does not change the value of the object that is passed to it”, so you will need to wrap your increment expressions in assignment expressions.

    BlockExpression block = Expression.Block(
        new ParameterExpression[] { 
            localvarnos1, localvarnos2, enumerator, currentelement },
        bexplocalnos1, 
        bexplocalnos2, 
        assignenumerator, 
        Expression.Loop(
            Expression.IfThenElse(
                Expression.NotEqual(movenext, Expression.Constant(false)),
                Expression.Block(
                    callCurrent, 
                    Expression.IfThenElse(
                        firstlessequalsecond, 
                        Expression.Assign(
                            localvarnos1, 
                            Expression.Increment(localvarnos1)), 
                        Expression.Assign(
                            localvarnos2, 
                            Expression.Increment(localvarnos2)))),
                Expression.Break(looplabel)), 
            looplabel),
        Expression.LessThan(localvarnos1, localvarnos2));
    
    Expression<Func<IEnumerable<int>, int, bool>> lambda = 
        Expression.Lambda<Func<IEnumerable<int>, int, bool>>(
            block,
            enumerableExpression,
            intexpression);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a dynamic forms app like wufoo . Creating new
I am trying to build a dynamic tree. I am getting my data from
I am trying to build a dynamic Property Accessor. Want something which is like
I am trying to build a dynamic sql query in java (shown below) sqlStr
I am trying to find the best way to build a dynamic linq query
Trying to build dynamic output from json and using jq/template tmpl display rows/columns. Somehow
I hope someone can help me..... i am trying to build a dynamic form
I am trying to build a real-time web service that deals with dynamic 10k
I'm trying to build an archetype structure like this, a webapp with some custom
I'm trying to pass context into a dynamic expression that I evaluate every iteration

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.