Convert IAsyncEnumerable to List

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)
        return results;

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">


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



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();

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