title: How to use GroupBy in an asynchronous manner in EF Core 3.1?
body: <p>When I use GroupBy as part of a LINQ query to EFCore, I get the error <code>System.InvalidOperationException: Client-side GroupBy is not supported</code>. </p>
  
  <p>This is because EF Core 3.1 attempts to evaluate queries on the server-side as much as possible, as opposed to evaluating them on the client-side, and the call cannot be translated to SQL.</p>
  
  <p>So the following statement does not work, and produces the error mentioned above:</p>
  
  <pre><code>var blogs = await context.Blogs
      .Where(blog =&gt; blog.Url.Contains("dotnet"))
      .GroupBy(t =&gt; t.BlobNumber)
      .Select(b =&gt; b)
      .ToListAsync();
  </code></pre>
  
  <p>Now apparently the solution is to use.AsEnumerable() or .ToList() before the call to GroupBy(), as that explicitly tells EF Core that you want to do the grouping client side. There is a discussion about this <a href="https://github.com/dotnet/efcore/issues/17068" rel="noreferrer">on GitHub</a> and <a href="https://docs.microsoft.com/en-us/ef/core/querying/client-eval#explicit-client-evaluation" rel="noreferrer">in the Microsoft docs</a>.</p>
  
  <pre><code>var blogs = context.Blogs
      .Where(blog =&gt; blog.Url.Contains("dotnet"))
      .AsEnumerable()
      .GroupBy(t =&gt; t.BlobNumber)
      .Select(b =&gt; b)
      .ToList();
  </code></pre>
  
  <p>However, this is not asynchronous. How can I make it asynchronous? </p>
  
  <p>If I change AsEnumerable() to AsAsyncEnumerable(), I get an error. If I instead try to change AsEnumerable() to ToListAsync() then the GroupBy() command fails. </p>
  
  <p>I am thinking of wrapping it in a Task.FromResult, but would this actually be asynchronous? Or is the database query still synchronous and only the subsequent grouping is asynchronous?</p>
  
  <pre><code>var blogs = await Task.FromResult(context.Blogs
      .Where(blog =&gt; blog.Url.Contains("dotnet"))
      .AsEnumerable()
      .GroupBy(t =&gt; t.BlobNumber)
      .Select(b =&gt; b)
      .ToList());
  </code></pre>
  
  <p>Or if that doesn't work is there another way?</p>