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

  • Home
  • SEARCH
  • 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 109681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:06:28+00:00 2026-05-11T02:06:28+00:00

I have two expressions of type Expression<Func<T, bool>> and I want to take to

  • 0

I have two expressions of type Expression<Func<T, bool>> and I want to take to OR, AND or NOT of these and get a new expression of the same type

Expression<Func<T, bool>> expr1; Expression<Func<T, bool>> expr2;  ...  //how to do this (the code below will obviously not work) Expression<Func<T, bool>> andExpression = expr AND expr2 
  • 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. 2026-05-11T02:06:29+00:00Added an answer on May 11, 2026 at 2:06 am

    Well, you can use Expression.AndAlso / OrElse etc to combine logical expressions, but the problem is the parameters; are you working with the same ParameterExpression in expr1 and expr2? If so, it is easier:

    var body = Expression.AndAlso(expr1.Body, expr2.Body); var lambda = Expression.Lambda<Func<T,bool>>(body, expr1.Parameters[0]); 

    This also works well to negate a single operation:

    static Expression<Func<T, bool>> Not<T>(     this Expression<Func<T, bool>> expr) {     return Expression.Lambda<Func<T, bool>>(         Expression.Not(expr.Body), expr.Parameters[0]); } 

    Otherwise, depending on the LINQ provider, you might be able to combine them with Invoke:

    // OrElse is very similar... static Expression<Func<T, bool>> AndAlso<T>(     this Expression<Func<T, bool>> left,     Expression<Func<T, bool>> right) {     var param = Expression.Parameter(typeof(T), 'x');     var body = Expression.AndAlso(             Expression.Invoke(left, param),             Expression.Invoke(right, param)         );     var lambda = Expression.Lambda<Func<T, bool>>(body, param);     return lambda; } 

    Somewhere, I have got some code that re-writes an expression-tree replacing nodes to remove the need for Invoke, but it is quite lengthy (and I can’t remember where I left it…)


    Generalized version that picks the simplest route:

    static Expression<Func<T, bool>> AndAlso<T>(     this Expression<Func<T, bool>> expr1,     Expression<Func<T, bool>> expr2) {     // need to detect whether they use the same     // parameter instance; if not, they need fixing     ParameterExpression param = expr1.Parameters[0];     if (ReferenceEquals(param, expr2.Parameters[0]))     {         // simple version         return Expression.Lambda<Func<T, bool>>(             Expression.AndAlso(expr1.Body, expr2.Body), param);     }     // otherwise, keep expr1 'as is' and invoke expr2     return Expression.Lambda<Func<T, bool>>(         Expression.AndAlso(             expr1.Body,             Expression.Invoke(expr2, param)), param); } 

    Starting from .NET 4.0, there is the ExpressionVisitor class which allows you to build expressions that are EF safe.

        public static Expression<Func<T, bool>> AndAlso<T>(         this Expression<Func<T, bool>> expr1,         Expression<Func<T, bool>> expr2)     {         var parameter = Expression.Parameter(typeof (T));          var leftVisitor = new ReplaceExpressionVisitor(expr1.Parameters[0], parameter);         var left = leftVisitor.Visit(expr1.Body);          var rightVisitor = new ReplaceExpressionVisitor(expr2.Parameters[0], parameter);         var right = rightVisitor.Visit(expr2.Body);          return Expression.Lambda<Func<T, bool>>(             Expression.AndAlso(left, right), parameter);     }        private class ReplaceExpressionVisitor         : ExpressionVisitor     {         private readonly Expression _oldValue;         private readonly Expression _newValue;          public ReplaceExpressionVisitor(Expression oldValue, Expression newValue)         {             _oldValue = oldValue;             _newValue = newValue;         }          public override Expression Visit(Expression node)         {             if (node == _oldValue)                 return _newValue;             return base.Visit(node);         }     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Turns out I was incorrectly calling LoadCursor(). If you're using… May 11, 2026 at 2:22 pm
  • added an answer Yes. NUnit works well on .NET 3.0 and 3.5 too.… May 11, 2026 at 2:22 pm
  • added an answer PDO::errorInfo() or PDOStatement->errorInfo() As for exceptions, check the docs for… May 11, 2026 at 2:22 pm

Related Questions

I want to compose the results of two Linq Expressions. They exist in the
There are 4 overloaded signatures for Enumerable.SelectMany. To make it simple, we ignore the
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
I have a function that takes a list that either has two or three

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.