title: How does this PredicateBuilder class work?
body: <p>I have been reading Joseph Albahari's brilliant book on C# 4.0 and I came across this class:</p>
  
  <pre><code>public static class PredicateBuilder
      {
          public static Expression&lt;Func&lt;T, bool&gt;&gt; True&lt;T&gt; () { return f =&gt; true; }
          public static Expression&lt;Func&lt;T, bool&gt;&gt; False&lt;T&gt; () { return f =&gt; false; }
  
          public static Expression&lt;Func&lt;T, bool&gt;&gt; Or&lt;T&gt; (this Expression&lt;Func&lt;T, bool&gt;&gt; expr1,
                                                    Expression&lt;Func&lt;T, bool&gt;&gt; expr2)
          {
              var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast&lt;Expression&gt; ());
  
              return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;
                   (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
          }
  
          public static Expression&lt;Func&lt;T, bool&gt;&gt; And&lt;T&gt; (this Expression&lt;Func&lt;T, bool&gt;&gt; expr1,
                                                     Expression&lt;Func&lt;T, bool&gt;&gt; expr2)
          {
              var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast&lt;Expression&gt; ());
              return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;
                   (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
          }
      }
  </code></pre>
  
  <p>Can anybody explain to me what this function is doing and how it works? I know it is used to add <code>and</code> and <code>or</code> conditions to the expression tree but how does it actually work? I have never used these classes like Expression and such. What is this specific code doing? </p>
  
  <pre><code>var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast&lt;Expression&gt; ());
  
                  return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;
                       (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
  </code></pre>
  
  <p>I know Func is a delegate which should return either true or false but what is this code in general doing ?</p>
  
  <p>Thanks in advance :)</p>