Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert IAsyncEnumerable to List

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?

like image 500
Twenty Avatar asked Dec 17 '19 18:12

Twenty


2 Answers

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";
    }
}
like image 159
Jon Skeet Avatar answered Nov 17 '22 06:11

Jon Skeet


On the off chance that you don't want to to bring in a NuGet package, here is (probably something similar to) the extension method mentioned in the package:

public static class AsyncEnumerableExtensions
{
    public static async Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> items,
        CancellationToken cancellationToken = default)
    {
        var results = new List<T>();
        await foreach (var item in items.WithCancellation(cancellationToken)
                                        .ConfigureAwait(false))
            results.Add(item);
        return results;
    }
}
like image 17
Jamie Twells Avatar answered Nov 17 '22 07:11

Jamie Twells