LINQ has very limited support for async
/await
. For LINQ-to-objects, the only really useful operation I know of is to do a Select
with an async
delegate (which results in a sequence of tasks).
List<T> data = new List<T>();
foreach (var id in ids)
data.Add(await LoadDataAsync(id));
If you can do LoadDataAsync
in parallel safely, your example could be rewritten as:
T[] data = await Task.WhenAll(ids.Select(id => LoadDataAsync(id)));