title: 'The parameter 'f' was not bound in the specified LINQ to Entities query expression'
body: <p>Using EF 6. Can someone tell me what am I doing wrong?</p>
  
  <pre><code>public List&lt;SearchResult&gt; SearchDocuments(List&lt;SearchCriteria&gt; searchCriterias)
  {
      List&lt;SearchResult&gt; results = new List&lt;SearchResult&gt;();
      var fieldSettings = LoadCoordinateSystemFieldSettings(searchCriterias);
      using (var context = CreateContext())
      {
          var tractsQuery = PredicateBuilder.False&lt;v_UploadByTract_ActiveUploads&gt;();
          foreach (var searchCriteria in searchCriterias)
          {
              var queryBuilder = PredicateBuilder.True&lt;v_UploadByTract_ActiveUploads&gt;();
  
              // tractsQuery
              if (searchCriteria.StateAPI.HasValue)
                  queryBuilder = queryBuilder.And(a =&gt; a.StateAPI == searchCriteria.StateAPI.Value);
              if (searchCriteria.CountyAPI.HasValue)
                  queryBuilder = queryBuilder.And(a =&gt; a.CountyAPI == searchCriteria.CountyAPI.Value);
  
              // ...
              // ... many more similar IF-ANDs
              // ...
  
              tractsQuery = tractsQuery.Or(queryBuilder);
          }
  
          var searchQuery = context.v_UploadByTract_ActiveUploads.AsExpandable().Where(tractsQuery).ToList();
          //.Select(a =&gt; SearchResult.Create(a));
          //results.AddRange(searchQuery.ToList());
      }
  
      return results;
  }
  
  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>