title: Func Delegates cause LINQ-to-Entities to pull back the entire table
body: <p>Passing a Func&lt;> as a Where/Count filter causes LINQ to pull back the entire table.  Here's a simple example.</p>
  
  <pre><code>pdx.Database.Log = strWriter1.Write;
  totalCount = pdx.Principals.Count(x =&gt; x.PrincipalNumber.ToLower().Contains("42"));
  </code></pre>
  
  <p>Looking at the log I see</p>
  
  <pre><code>SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT  COUNT(1) AS [A1]
     FROM [Dealer].[Principal] AS [Extent1] 
  WHERE LOWER([Extent1].[PrincipalNumber]) LIKE N'%42%'
  )  AS [GroupBy1]
  </code></pre>
  
  <p>Did not pull back the full table.  Simple enough.  Now let's assign that lambda to a Func&lt;></p>
  
  <pre><code>pdx.Database.Log = strWriter2.Write;
  Func&lt;Principal, bool&gt; filter = (x =&gt; x.PrincipalNumber.ToLower().Contains("42"));
  totalCount = pdx.Principals.Count(filter);
  </code></pre>
  
  <p>The log shows it's pulling down the entire table.</p>
  
  <pre><code>SELECT 
  [Extent1].[PrincipalNumber] AS [PrincipalNumber], 
  [Extent1].[Id] AS [Id], 
  [Extent1].[CompanyName] AS [CompanyName], 
  ...
  [Extent1].[DistrictSalesManagerId] AS [DistrictSalesManagerId]
  FROM [Dealer].[Principal] AS [Extent1]
  </code></pre>
  
  <p>That's pretty bad for performance.  I have functions that do LINQ queries.  I want to pass lambda filters to these functions so I can filter on various things, but apparently I can't pass lambdas as Func&lt;>s because it will kill the performance.  What are my alternatives?</p>
  
  <p>What I want to do...</p>
  
  <pre><code>public IEnumerable&lt;DealerInfo&gt; GetMyPage(Func&lt;Principal, bool&gt; filter, int pageNumber, int pageSize, out int totalCount)
  {
      List&lt;DealerInfo&gt; dealers;
  
      using (MyContext pdx = new MyContext())
      {
          totalCount = pdx.Principals.Count(filter);
          // More LINQ stuff here, but UGH the performance...
      }
  }
  </code></pre>