Matt Johnson Matt Johnson - 1 year ago 88
C# Question

How to await a list of tasks asynchronously using LINQ?

I have a list of tasks that I created like this:

public async Task<IList<Foo>> GetFoosAndDoSomethingAsync()
var foos = await GetFoosAsync();

var tasks = foos.Select(async foo => await DoSomethingAsync(foo)).ToList();


By using
, the tasks should all start. Now I want to await their completion and return the results.

This works in the above

var list = new List<Foo>();
foreach (var task in tasks)
list.Add(await task);
return list;

It does what I want, but this seems rather clumsy. I'd much rather write something simpler like this:

return tasks.Select(async task => await task).ToList();

... but this doesn't compile. What am I missing? Or is it just not possible to express things this way?

Answer Source

LINQ doesn't work perfectly with async code, but you can do this:

var tasks = foos.Select(DoSomethingAsync).ToList();
await Task.WhenAll(tasks);

If your tasks all return the same type of value, then you can even do this:

var results = await Task.WhenAll(tasks);

which is quite nice. WhenAll returns an array, so I believe your method can return the results directly:

return await Task.WhenAll(tasks);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download