I'm using Task.WhenAll
to call multiple async requests with different parameters to get patients
of various pharmacies
by selecting patient ids of that pharmacy.
And I want to add this data to one list and then map it to other models.
Here is my code.
var patients = new List<PatientDTO>();
await Task.WhenAll(
pharmacyIds.Select(async pharmacyId =>
patients.AddRange(await _patientService.GetPatientsByClientIds(pharmacyId, recipes
.Where(x => x.PharmacyId == pharmacyId)
.Select(x => x.PatientId)))));
I'm getting this data paginated in my request, and my page size is often 50
most of the time, this works correctly, but sometimes it throws an exception with the message:
Destination array was not long enough. Check the destination index, length, and the array's lower bounds. (Parameter 'destinationArray')
.
Here is some more information about the exception.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.Generic.List`1.Insert(Int32 index, T item)
at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
at Coach.Domain.Queries.GetRecipeLogsQueryHandler.<>c__DisplayClass4_0.<<Handle>b__14>d.MoveNext()
Where is my mistake, and how can I solve this?
- NOTE: I want multiple requests to be async, and I don't want to call them in sequence.