I have a list of tasks that I created as follows:
public async Task<IList<Foo>> GetFoosAndDoSomethingAsync() { var foos = await GetFoosAsync(); var tasks = foos.Select(async foo => await DoSomethingAsync(foo)).ToList(); ... }
Using .ToList() , all tasks must begin. Now I want to wait for them to complete and return the results.
It works in the previous block ... :
var list = new List<Foo>(); foreach (var task in tasks) list.Add(await task); return list; (); var list = new List<Foo>(); foreach (var task in tasks) list.Add(await task); return list;
He does what I want, but it seems quite awkward. I would rather have written something more simple:
return tasks.Select(async task => await task).ToList();
... but it will not compile. What am I missing? Or is it just impossible to express it?
# c linq the async-the await
Matt Johnson 18 February. '14 at 23:52 18/02/2014 23:52
source share