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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:30:15+00:00 2026-05-20T18:30:15+00:00

I want to combine two LambdaExpressions without compiling them. This is what it looks

  • 0

I want to combine two LambdaExpressions without compiling them.

This is what it looks like if I do compile them:

    public Expression<Func<TContainer,bool>> CreatePredicate<TContainer,TMember>(
        Expression<Func<TContainer,TMember>> getMemberExpression, 
        Expression<Func<TMember,bool>> memberPredicateExpression)
    {
        return x => memberPredicateExpression.Compile()(getMemberExpression.Compile()(x));
    }

That’s obviously not the fastest way to get the target expression from the provided arguments. Also, it makes it incompatible with query providers like LINQ to SQL that do not support C# method calls.

From what I’ve read it seems like the best approach is to build an ExpressionVisitor class. However, this seems like it could be a pretty common task. Does anyone know of an existing open source code base that provides this kind of functionality? If not, what is the best way to approach the ExpressionVisitor to make it as generic as possible?

  • 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-20T18:30:15+00:00Added an answer on May 20, 2026 at 6:30 pm

    I don’t know if it’s the best way, but you could do something like that:

    public Expression<Func<TContainer,bool>> CreatePredicate<TContainer,TMember>(
        Expression<Func<TContainer,TMember>> getMemberExpression, 
        Expression<Func<TMember,bool>> memberPredicateExpression)
    {
        ParameterExpression x = Expression.Parameter(typeof(TContainer), "x");
        return Expression.Lambda<Func<TContainer, bool>>(
            Expression.Invoke(
                memberPredicateExpression,
                Expression.Invoke(
                    getMemberExpression,
                    x)),
            x);
    }
    

    Usage:

    var expr = CreatePredicate(
        (Foo f) => f.Bar,
        bar => bar % 2 == 0);
    

    Result:

    x => Invoke(bar => ((bar % 2) == 0), Invoke(f => f.Bar, x))
    

    I guess it would be better to get something like x => x.Bar % 2 == 0, but it would probably be significantly harder…


    EDIT: actually it wasn’t so hard with an expression visitor:

    public Expression<Func<TContainer,bool>> CreatePredicate<TContainer,TMember>(
        Expression<Func<TContainer,TMember>> getMemberExpression, 
        Expression<Func<TMember,bool>> memberPredicateExpression)
    {
        return CombineExpressionVisitor.Combine(
            getMemberExpression,
            memberPredicateExpression);
    }
    
    class CombineExpressionVisitor : ExpressionVisitor
    {
        private readonly ParameterExpression _parameterToReplace;
        private readonly Expression _replacementExpression;
        private CombineExpressionVisitor(ParameterExpression parameterToReplace, Expression replacementExpression)
        {
            _parameterToReplace = parameterToReplace;
            _replacementExpression = replacementExpression;
        }
    
        public static Expression<Func<TSource, TResult>> Combine<TSource, TMember, TResult>(
            Expression<Func<TSource, TMember>> memberSelector,
            Expression<Func<TMember, TResult>> resultSelector)
        {
             var visitor = new CombineExpressionVisitor(
                resultSelector.Parameters[0],
                memberSelector.Body);
            return Expression.Lambda<Func<TSource, TResult>>(
                visitor.Visit(resultSelector.Body),
                memberSelector.Parameters);
        }
    
        protected override Expression VisitParameter(ParameterExpression parameter)
        {
            if (parameter == _parameterToReplace)
                return _replacementExpression;
            return base.VisitParameter(parameter);
        }
    }
    

    It gives the following expression:

    f => ((f.Bar % 2) == 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Firebird, I want to combine the results of two queries using UNION ALL,
I want to combine two array's, excluding duplicates. I am using a custom class:
I want to combine two relative paths in C#. For example: string path1 =
In Clojure, I want to combine two lists to give a list of pairs,
I have a database for a Train company. I want to combine two queries
This question is somewhat similar to How to combine two branches from two different
I want to combine these two queries into one: mysql_query(INSERT INTO categories (name, parent)
I want to combine these: keys = ['name', 'age', 'food'] values = ['Monty', 42,
In a table I want to combine some of the columns, but not in
I want to do a select in MySql that combines several columns... something like

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.