1

I am not getting how to combine two list producing tasks into a common list. The below code is incomplete and/or wrong, but I think will get my dilemma across. The OrderFactory requests data from an Internet server. Each OrderFactory completes at some future time and independent of any other OrderFactory. How can the List orders returned from each task be combined into one list? Both tasks are to start together but will return at a different future time.

TIA

        private async Task<List<Orders>> GetReturnToOfficeOrdersAsync(PatientService patient)
        {
            var orders = new List<Orders>();
            var x =  await OrderFactory.CreateOrders(OrderType.RETURNTOOFFICE, patient);
            orders.AddRange(x);
            return orders;
        }

        private async Task<List<Orders>> GetProceduresAsync(PatientService patient)
        {
            var orders = new List<Orders>();
            var x = await OrderFactory.CreateOrders(OrderType.PROCEDURE, patient);
            orders.AddRange(x);
            return orders;
        }


        private async Task GetInCompleteOrderListAsync()
        {
            if (patient == null) return;

            var orders = new List<Orders>();

*** THE LISTS FROM THESE TASKS NEED TO BE COMBINED INTO ONE LIST -- HOW???    
            var a = GetReturnToOfficeOrdersAsync(patient);
            var b = GetProceduresAsync(patient);
            await Task.WhenAll(a, b);

            orders = orders.OrderByDescending(o => o.torder).ToList();

            IncompleteOrders = new ObservableCollection<Orders>(orders);
        }
Alan Wayne
  • 5,122
  • 10
  • 52
  • 95
  • 1
    What's wrong with simply `orders.AddRange(a.Result); orders.AddRange(b.Result);`? – Sami Kuhmonen Sep 18 '16 at 04:44
  • @SamiKuhmonen Ugh. Nothing. Also found answer at http://stackoverflow.com/questions/17197699/awaiting-multiple-tasks-with-different-results. Thanks. – Alan Wayne Sep 18 '16 at 04:47
  • 1
    Since there is nothing asynchronous about combining two or more lists - standard approaches to combine lists apply (hence duplicate). To get the results of multiple asynchronous operations use link provided by @AlanWayne http://stackoverflow.com/questions/17197699/awaiting-multiple-tasks-with-different-results. – Alexei Levenkov Sep 18 '16 at 04:59

1 Answers1

2

If you want to add results of tasks a and b to orders, you can use method AddRange:

var orders = new List<Orders>();

var a = GetReturnToOfficeOrdersAsync(patient);
var b = GetProceduresAsync(patient);

orders.AddRange(await a);    // add result of task a to the list
orders.AddRange(await b);    // add result of task b to the list
dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • why not `await Task.WhenAll(a,b)`, this will make calling `tasks a,b` serial – Mrinal Kamboj Sep 18 '16 at 18:02
  • The calls are not serial since it is not awaiting them immediately. Variables are a and b are tasks that are kicked off immediately. Then both results are awaited after the tasks have been created. – Tim Wilson Sep 02 '22 at 16:21