I have a legacy code in which ids
List has some duplicate value coming and this method is being called from lot of difference places as of now -
public async Task<List<T>> Execute<T>(IList<int> ids, Policy policy, Func<CancellationToken, int, Task<T>> mapperFunc) where T : class
{
var holder = new List<Task<T>>(ids.Count);
var removeNull = new List<T>(ids.Count);
for (int i = 0; i < ids.Count; i++)
{
var id = ids[i];
holder.Add(ProcessData(policy, ct => mapperFunc(ct, id)));
}
var responses = await Task.WhenAll(holder);
for (int i = 0; i < responses.Length; i++)
{
var response = responses[i];
if (response != null)
{
removeNull.Add(response);
}
}
return removeNull;
}
I am trying to change above method such that I can remove duplicate stuff from ids
list so I came up with below code which does that but I wanted to see if there is any better way we can write below code?
public async Task<List<T>> Execute<T>(IList<int> ids, Policy policy, Func<CancellationToken, int, Task<T>> mapperFunc) where T : class
{
var noDupsList = new HashSet<int>(ids).ToList();
var holder = new List<Task<T>>(noDupsList.Count);
var removeNull = new List<T>(noDupsList.Count);
for (int i = 0; i < noDupsList.Count; i++)
{
var id = noDupsList[i];
holder.Add(ProcessData(policy, ct => mapperFunc(ct, id)));
}
var responses = await Task.WhenAll(holder);
for (int i = 0; i < responses.Length; i++)
{
var response = responses[i];
if (response != null)
{
removeNull.Add(response);
}
}
return removeNull;
}