Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It was difficult for me to understand hvd's <a href="https://stackoverflow.com/questions/9231569/exception-using-orelse-and-andalso-expression-methods?answertab=active#tab-top/" title="hvds answer">answer</a> so I created some code to explain it in a different way. hvd should get the credit for suggesting the ExpressionVisitor. I just couldn't understand the example in the context of Linq to X type input functions I was using.</p> <p>I hope this helps somebody else coming to the question from that perspective.</p> <p>Also, I created the combining code as extension methods to make it a little easier to use.</p> <hr> <pre><code>using System; using System.Collections.Generic; using System.Linq.Expressions; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { var combined = TryCombiningExpressions(c =&gt; c.FirstName == "Dog", c =&gt; c.LastName == "Boy"); Console.WriteLine("Dog Boy should be true: {0}", combined(new FullName { FirstName = "Dog", LastName = "Boy" })); Console.WriteLine("Cat Boy should be false: {0}", combined(new FullName { FirstName = "Cat", LastName = "Boy" })); Console.ReadLine(); } public class FullName { public string FirstName { get; set; } public string LastName { get; set; } } public static Func&lt;FullName, bool&gt; TryCombiningExpressions(Expression&lt;Func&lt;FullName, bool&gt;&gt; func1, Expression&lt;Func&lt;FullName, bool&gt;&gt; func2) { return func1.CombineWithAndAlso(func2).Compile(); } } public static class CombineExpressions { public static Expression&lt;Func&lt;TInput, bool&gt;&gt; CombineWithAndAlso&lt;TInput&gt;(this Expression&lt;Func&lt;TInput, bool&gt;&gt; func1, Expression&lt;Func&lt;TInput, bool&gt;&gt; func2) { return Expression.Lambda&lt;Func&lt;TInput, bool&gt;&gt;( Expression.AndAlso( func1.Body, new ExpressionParameterReplacer(func2.Parameters, func1.Parameters).Visit(func2.Body)), func1.Parameters); } public static Expression&lt;Func&lt;TInput, bool&gt;&gt; CombineWithOrElse&lt;TInput&gt;(this Expression&lt;Func&lt;TInput, bool&gt;&gt; func1, Expression&lt;Func&lt;TInput, bool&gt;&gt; func2) { return Expression.Lambda&lt;Func&lt;TInput, bool&gt;&gt;( Expression.AndAlso( func1.Body, new ExpressionParameterReplacer(func2.Parameters, func1.Parameters).Visit(func2.Body)), func1.Parameters); } private class ExpressionParameterReplacer : ExpressionVisitor { public ExpressionParameterReplacer(IList&lt;ParameterExpression&gt; fromParameters, IList&lt;ParameterExpression&gt; toParameters) { ParameterReplacements = new Dictionary&lt;ParameterExpression, ParameterExpression&gt;(); for (int i = 0; i != fromParameters.Count &amp;&amp; i != toParameters.Count; i++) ParameterReplacements.Add(fromParameters[i], toParameters[i]); } private IDictionary&lt;ParameterExpression, ParameterExpression&gt; ParameterReplacements { get; set; } protected override Expression VisitParameter(ParameterExpression node) { ParameterExpression replacement; if (ParameterReplacements.TryGetValue(node, out replacement)) node = replacement; return base.VisitParameter(node); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload