Convert IAsyncEnumerable to List

Twenty picture Twenty · Dec 17, 2019 · Viewed 17.2k times · Source

So in C#8 we got the addition of the IAsyncEnumerable interface.

If we have a normal IEnumerable we can make a List or pretty much any other collection we want out of it. Thanks to Linq there.

var range = Enumerable.Range(0, 100);
var list = range.ToList();

Well now I want to convert my IAsyncEnumerable to a List and this of course asynchronously. Are there already Linq implementations for that case? If there isn't, how could I convert it myself then?

Answer

Jon Skeet picture Jon Skeet · Dec 17, 2019

Sure - you just need the ToListAsync() method, which is in the System.Linq.Async NuGet package. Here's a complete example:

Project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Linq.Async" Version="4.0.0" />
  </ItemGroup>

</Project>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        IAsyncEnumerable<string> sequence = GetStringsAsync();
        List<string> list = await sequence.ToListAsync();
        Console.WriteLine(list.Count);
    }

    static async IAsyncEnumerable<string> GetStringsAsync()
    {
        yield return "first";
        await Task.Delay(1000);
        yield return "second";
        await Task.Delay(1000);
        yield return "third";
    }
}