Rather than awaiting each task, add them to a list and then use
Task.WhenAll[
^] to wait for them all to complete:
public static async Task<List<createReservation>> GetByLastDate(string specificUrl, string token, DateTime date)
{
string fullUrl = baseUrl + specificUrl+ date;
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, fullUrl))
{
requestMessage.Headers.Add("access_token", token);
using (HttpResponseMessage res = await policy.ExecuteAsync(() => client.SendAsync(requestMessage)))
{
string data = await res.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(data)) return null;
return JsonConvert.DeserializeObject<List<createReservation>>(response);
}
}
}
var tasks = new List<Task<List<createReservation>>>(dt.Rows.Count);
foreach (DataRow item in dt.Rows)
{
tasks.Add(RestApIHelper.GetByLastDate(item["Url"].ToString(),
fetchAllFirstTime.token, Convert.ToDateTime(item["LastDate"]));
}
IEnumerable<List<createReservation>> results = await Task.WhenAll(tasks);
foreach (List<createReservation> data in results)
{
roomCreateOrUpdateViaAPI(data, "insert");
}