If we want to use a method that’s marked as async
inside a lambda expression, we have to split it in 2 steps:
- task declaration
- (async/await) task execution
// task declaration
var mapTask = animals.Select(Map).ToList();
// task execution
var animalsMapped = (await Task.WhenAll(mapTask)).ToList();
// mapping method
private async Task<Animal> Map(Animal animal)
{
// ... do whatever mapping is needed
}