Skip to content

Instantly share code, notes, and snippets.

@dhei
Last active June 28, 2023 16:11
Show Gist options
  • Save dhei/39a2d7ce3e82e83266f6bd40eccb1468 to your computer and use it in GitHub Desktop.
Save dhei/39a2d7ce3e82e83266f6bd40eccb1468 to your computer and use it in GitHub Desktop.

Example of LINQ Select and ToDictionary with parallel execution of tasks

var dataSource = new List<object>() { "aaa" , "bbb" };
var tasks = dataSource.Select(async data => new { Key = data.ToString(), Value = await AsyncDoSomething(data.ToString()) });
var results = await Task.WhenAll(tasks);
Dictionary<string, int> dictionary = results.ToDictionary(pair => pair.Key, pair => pair.Value);

Adapted from this Stackoverflow answer.


Reference:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment