title: Entity Framework DbSet<TEntity>.Where(e => true) performance
body: <p>I am using Entity Framework 6 to access a database.</p>
  
  <p>Is there a performance difference between the following two methods?</p>
  
  <pre><code>public IEnumerable&lt;TEntity&gt; GetAll()
  {
      using (var context = new DbContext())
          return context.Set&lt;TEntity&gt;().ToList();
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public IEnumerable&lt;TEntity&gt; GetAll()
  {
      using (var context = new DbContext())
          return context.Set&lt;TEntity&gt;().Where(e =&gt; true).ToList();
  }
  </code></pre>
  
  <p>I am asking because I would like to use conditional predicates. Something like this the following.</p>
  
  <pre><code>public IEnumerable&lt;TEntity&gt; GetAll(TKey fKey)
  {
      using (var context = new DbContext())
          return context.Set&lt;TEntity&gt;()
                        .Where(e =&gt; fKey != null ? e.fKey == fKey : true).ToList();
  }
  </code></pre>
  
  <p>where <code>fKey</code> would be a foreign key.</p>
  
  <p><strong>Update</strong>: Since many comments focus on my last example, that I agree is a bad use case, I will explain why I am asking.</p>
  
  <p>So I have repository method that looks something like the following.</p>
  
  <pre><code>public class EntityRepository
  {
      public IEnumerable&lt;Entity&gt; GetAll(Expression&lt;Func&lt;Entity, bool&gt;&gt; predicate)
      {
          using (var context = new DbContext())
              return context.Set&lt;Entity&gt;.Where(predicate).ToList();
      }
  }
  </code></pre>
  
  <p>And I am using this method form a Web API controller's action-method. Something like the following.</p>
  
  <pre><code>public IHttpActionResult GetEntities(string param1, string param2, string param3)
  {
      Expression&lt;Func&lt;Entity, bool&gt;&gt; predicate = e =&gt;
           (param1 != null ? e.field1 == param1 : true)
           &amp;&amp; (param2 != null ? e.field2 == param2 : true)
           &amp;&amp; (param3 != null ? e.field3 == param3 : true);
      var entities = EntityRepository.GetAll(predicate);
      return Ok(entities);
  }
  </code></pre>
  
  <p>So here I get some query parameters from the URI and I create the predicate based on them. Some of these parameters can be <code>null</code> in which case I don't want to filter on those. But I don't want to create a different predicate for all the combinations of the parameters being <code>null</code>s or not.</p>
  
  <p>I know I could read the entire set and filter one-by-one afterwards, but that would use a lot of memory with big data sets.</p>
  
  <p><strong>So just to clarify my question</strong>: Is this a right approach? If all 3 parameters are <code>null</code> (in which case the entire set will be returned) does this method cause any performance setbacks?</p>