I have an async method that gets a List
of component ids for a car id. I want to get component ids for several hundred cars asynchronously, so I wrote t
If you changed the declaration of taskList
to use the generic version of Task
, Task<T>
that represents a future value of type T
...
List<Task<List<long>>> taskList
now the task's result is of type List<long>
rather than void
, and compiler type inference will switch you to a different overload of Task.WhenAll
, Task.WhenAll<TResult>(Task<TResult> tasks).
Creates a task that will complete when all of the Task objects in an enumerable collection have completed
[...]
The
Task<TResult>.Result
property of the returned task will be set to an array containing all of the results of the supplied tasks in the same order as they were provided (e.g. if the input tasks array containedt1, t2, t3
, the output task'sTask<TResult>.Result
property will return anTResult[]
wherearr[0] == t1.Result, arr[1] == t2.Result
, andarr[2] == t3.Result
).
so, as the type of Task
is List<long>
, the following statement is how you'd access the collated result array:
List<Long>[] resultLists = await Task.WhenAll(tasksList);