I have a Post Method that is currently synchronous:
[HttpPost]
public ActionResult DownloadSelectedDetails(int[] selectedRows)
{
var orderPlanViews =
selectedRows.Select(orderPlanId =>
_orderManager.GetOrderPlanView(orderPlanId)).ToList();
var model = new DownloadDetailViewModel
{
OrderPlanViews = orderPlanViews,
DownloadConfirmed = false,
};
return PartialView("_DownloadDetail", model);
}
Now I'd like to make this method use the asynchronous Method _orderManager.GetOrderPlanViewAsync. The Interface Definition of it is:
Task<OrderPlanView> GetOrderPlanViewAsync(int orderPlanId);
Therefore I changed it to:
[HttpPost]
public ActionResult DownloadSelectedDetails(int[] selectedRows)
{
var orderPlanViews =
selectedRows.Select(async (orderPlanId) =>
{
await _orderManager.GetOrderPlanViewAsync(orderPlanId);
}).ToList();
var model = new DownloadDetailViewModel
{
OrderPlanViews = orderPlanViews,
DownloadConfirmed = false,
};
return PartialView("_DownloadDetail", model);
}
But the compiler tells me:
Cannot implicitly convert type System.Collections.Generic.List< System.Threading.Tasks.Task > to System.Collections.Generic.List< OrderPlanView >
I think the difference to the proposed duplicate question is, that I have a list of Tasks.
After more googleing and takeing a break, I found this question: Transform IEnumerable<Task<T>> asynchronously by awaiting each task
that helped me!