'How to hydrate a Dictionary with the results of async calls?
Suppose I have code that looks like this:
public async Task<string> DoSomethingReturnString(int n) { ... }
int[] numbers = new int[] { 1, 2 , 3};
Suppose that I want to create a dictionary that contains the result of calling DoSomethingReturnString
for each number similar to this:
Dictionary<int, string> dictionary = numbers.ToDictionary(n => n,
n => DoSomethingReturnString(n));
That won't work because DoSomethingReturnString returns Task<string>
rather than string
. The intellisense suggested that I try specifying my lambda expression to be async, but this didn't seem to fix the problem either.
Solution 1:[1]
If you insist on doing it with linq, Task.WhenAll
is the key to "hydrate" the dictionary:
int[] numbers = new int[] { 1, 2 , 3};
KeyValuePair<int, string>[] keyValArray = //using KeyValuePair<,> to avoid GC pressure
await Task.WhenAll(numbers.Select(async p =>
new KeyValuePair<int, string>(p, await DoSomethingReturnString(p))));
Dictionary<int, string> dict = keyValArray.ToDictionary(p => p.Key, p => p.Value);
Solution 2:[2]
LINQ methods do not support asynchronous actions (e.g., asynchronous value selectors), but you can create one yourself. Here is a reusable ToDictionaryAsync
extension method that supports an asynchronous value selector:
public static class ExtensionMethods
{
public static async Task<Dictionary<TKey, TValue>> ToDictionaryAsync<TInput, TKey, TValue>(
this IEnumerable<TInput> enumerable,
Func<TInput, TKey> syncKeySelector,
Func<TInput, Task<TValue>> asyncValueSelector)
{
Dictionary<TKey,TValue> dictionary = new Dictionary<TKey, TValue>();
foreach (var item in enumerable)
{
var key = syncKeySelector(item);
var value = await asyncValueSelector(item);
dictionary.Add(key,value);
}
return dictionary;
}
}
You can use it like this:
private static async Task<Dictionary<int,string>> DoIt()
{
int[] numbers = new int[] { 1, 2, 3 };
return await numbers.ToDictionaryAsync(
x => x,
x => DoSomethingReturnString(x));
}
Solution 3:[3]
This is just a combination of @Yacoub's and @David's answers for an extension method which uses Task.WhenAll
public static async Task<Dictionary<TKey, TValue>> ToDictionaryAsync<TInput, TKey, TValue>(
this IEnumerable<TInput> enumerable,
Func<TInput, TKey> syncKeySelector,
Func<TInput, Task<TValue>> asyncValueSelector) where TKey : notnull
{
KeyValuePair<TKey, TValue>[] keyValuePairs = await Task.WhenAll(
enumerable.Select(async input => new KeyValuePair<TKey, TValue>(syncKeySelector(input), await asyncValueSelector(input)))
);
return keyValuePairs.ToDictionary(pair => pair.Key, pair => pair.Value);
}
Solution 4:[4]
If calling from an asynchronous method, you can write a wrapper method that creates a new dictionary and builds a dictionary by iterating over each number, calling your DoSomethingReturnString
in turn:
public async Task CallerAsync()
{
int[] numbers = new int[] { 1, 2, 3 };
Dictionary<int, string> dictionary = await ConvertToDictionaryAsync(numbers);
}
public async Task<Dictionary<int, string>> ConvertToDictionaryAsync(int[] numbers)
{
var dict = new Dictionary<int, string>();
for (int i = 0; i < numbers.Length; i++)
{
var n = numbers[i];
dict[n] = await DoSomethingReturnString(n);
}
return dict;
}
Solution 5:[5]
var tempDictionary = numbers.ToDictionary(n => n,
n => DoSomethingReturnString(n));
await Task.WhenAll(tempDictionary.Value).ConfigureAwait(false);
var dictionary = tempDictionary.ToDictionary(k => k.Key, v => v.Value.Result);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Patrick Szalapski |
Solution 4 | David L |
Solution 5 |