I'm incrementally introducing Ix.NET into a legacy project.  I have a number of storage-level APIs that return Task<IEnumerable<T>>, but I want to adapt them to IAsyncEnumerable<T> for consumption in the rest of the system.  It seems like there should be a helper method (ala .ToAsyncEnumerable() for IEnumerable) to help with this, but I can't find anything...  Do I have to implement my own custom Enumerator?  (not hard, but I don't want to re-invent the wheel)
How can I "adapt" a Task<IEnumerable<T>> to IAsyncEnumerable<T>?
10.5k Views Asked by Kevin Halverson At
        	5
        	
        There are 5 best solutions below
6
                 On
                        
                            
                        
                        
                            On
                            
                                                    
                    
                If you're talking about web APIs, Task<IEnumerable<T>> is an asynchronous way of producing a IEnumerable<T>.
Regardless of that IEnumerable<T> being produced synchronously or asynchronously, the whole list will be sent as an HTTP response.
The way you could leverage IAsyncEnumerable<T> on the client is if that client is invoking some kind of streaming or making multiple requests to a server for a unique list of results (paging).
0
                 On
                        
                            
                        
                        
                            On
                            
                                                    
                    
                Task<IEnumerable<T>> GetSomeResults<T>()
{
    throw new NotImplementedException();
}
async IAsyncEnumerable<T> GetAsyncEnumerable<T>()
{
    var results = await GetSomeResults<T>();
    foreach(var item in results)
    {
        yield return item;
    }
}
0
                 On
                        
                            
                        
                        
                            On
                            
                                                    
                    
                As commented by Theodor Zoulias,
System.Linq.Async is a NuGet package from .NET Foundation, which supports ToAsyncEnumerable().
Example usage:
    var tasks = new Task[0]; // get your IEnumerable<Task>s
    tasks.ToAsyncEnumerable();
I was looking for the exact same thing, and due to the replies here I assume that there is indeed no method like
AsAsyncEnumerable(). So here's what I ended up doing, maybe it helps someone else: