1

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!

Community
  • 1
  • 1
less
  • 699
  • 6
  • 25
  • 3
    `async` will make the anonymous method return a `Task`. You have to await that one. – Patrick Hofman Mar 19 '15 at 14:12
  • ok, How do i do that? – less Mar 19 '15 at 14:12
  • See the duplicate question. – Patrick Hofman Mar 19 '15 at 14:15
  • You can show you code of the method `GetOrderPlanView` –  Mar 19 '15 at 14:15
  • I saw those posts, but didn't get it right yet. I added Interface definition for GetOrderPlanView in Post – less Mar 19 '15 at 14:18
  • so I tried: await selectedRows.Select(async orderPlanId =>... but then it tells me that it's not awaitable. Still don't get it – less Mar 19 '15 at 14:26
  • @less You have a list of tasks, not a single task. You await a *single* task. – Servy Mar 19 '15 at 15:48
  • @Servy yes, that's what i wrote in my update of the post. Can you provide me with an elegant solution? – less Mar 19 '15 at 15:59
  • Looks like you already have your solution (`WhenAll`) you just need to use it. – Servy Mar 19 '15 at 16:00
  • Ok, that's what I did. It compiles but I can't try out right now if it works in real world :-) Thanks anyway! – less Mar 19 '15 at 16:02
  • Unfortunately it does not work in real world. I get the following error: Not Supported Exception Additional information: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe. – less Apr 15 '15 at 09:00

0 Answers0