我有一个当前同步的Post方法:
[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);
}
现在我想让这个方法使用异步方法_orderManager.GetOrderPlanViewAsync。它的接口定义是:
Task<OrderPlanView> GetOrderPlanViewAsync(int orderPlanId);
因此我改为:
[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);
}
但是编译器告诉我:
无法隐式转换类型System.Collections.Generic.List&lt; System.Threading.Tasks.Task&gt;到System.Collections.Generic.List&lt; OrderPlanView&gt;
我认为提出的重复问题的不同之处在于,我有一个任务列表。
经过更多googleing并休息一下后,我发现了这个问题: Transform IEnumerable<Task<T>> asynchronously by awaiting each task
帮了我!