title: What happens to the WHERE clause with IAsyncEnumerable in Entity Framework Core?
body: <p>I'm porting an existing application that uses Entity Framework with a SQL Server database from .NET Framework to .NET Core. In EF6, you could do:</p>
  <pre><code>private async Task&lt;IEnumerable&lt;T&gt;&gt; GetByExpressionAsync(Expression&lt;Func&lt;T, bool&gt;&gt; where))
  {
      return await _context.DbSet&lt;T&gt;().Where(where).ToListAsync();
  }
  </code></pre>
  <p>Assuming that the <code>where</code> expression could be evaluated server-side, this would translate to a SQL query like <code>SELECT columns FROM table WHERE Foo=1</code>.</p>
  <p>This is the same method in EF Core. Yes, I realize I'm not taking advantage of async streams, but remember this is an existing application and I don't have the time to really redesign it.</p>
  <pre><code>private async Task&lt;IEnumerable&lt;T&gt;&gt; GetByExpressionAsync(Expression&lt;Func&lt;T, bool&gt;&gt; where))
  {
      return await _context.DbSet&lt;T&gt;().AsAsyncEnumerable().Where(where.Compile()).ToListAsync();
  }
  </code></pre>
  <p>The table that I'm having the problem with has hundreds of thousands of records. If I use SQL Profiler, I can see that there is no <code>WHERE</code> clause, so SQL Server is returning the entire table, which is of course so not ideal. I didn't notice at first because my unit tests run against a much smaller table so there was no indication there would be a performance issue.</p>
  <p>According to <a href="https://docs.microsoft.com/en-us/ef/core/querying/how-query-works" rel="nofollow noreferrer">the Microsoft documentation</a>, &quot;The database provider identifies which parts of the query can be evaluated in the database&quot; and &quot;These parts of the query are translated to database-specific query language&quot;. So when I use simple <code>[column] == [value]</code> filters, this should work properly, and indeed, the synchronous works exactly as expected.</p>
  <pre><code>private IEnumerable&lt;T&gt; GetByExpression(Expression&lt;Func&lt;T, bool&gt;&gt; where))
  {
      return _context.DbSet&lt;T&gt;().AsQueryable().Where(where).ToList();
  }
  </code></pre>
  <p>What am I doing wrong? Thanks in advance.</p>