I am trying to iterate over a list and trying to get data from async method. Following is the code :
var tasks = listOfStrings.Select(async currentString => {
//creating an object RequestDataObj
RequestDataObj request = new RequestDataObj();
//filling RequestDataObj with some request data based on currentString
//making async call and getting data in resultDataObj
var resultDataObj = await ApiCall.GetDataAsync(request, currentString);
//saving resultDataObj + currentString to database synchronously
});
await Task.WhenAll(tasks);
//doing some other operations here
Problem is that in database the value of currentString is always the last item of listOfStrings. Am I doing anything wrong? Please help.
Thanks