LINQ: Beware of deferred execution
If you've spent much time around C# and .NET, it's likely that you will have come across LINQ (Language-Integrated Query), which allows you to use a range of powerful querying capabilities directly in the C# language.
The example below demonstrate a ...
samwalpole.com6 min read
Thanks for the great article!
I've kept the tab open for some time, because I felt like I should point out one thing and wasn't sure what to say exactly.
If you would change your code sample describing the problem with an IEnumerable of Tasks, it would already run properly without the usage of ToList().
var listOfIds = new List<int> { 1, 5, 8, 15 }; var tasks = listOfIds.Select(id => _repository.GetAsync(id)); var results = await Task.WhenAll(tasks);That's because Task.WhenAll(...) returns a Task with an array of all results as its result.
I get that your code sample simply depicts an issue that would occur when you don't properly evaluate the IEnumerable, the provided solution just didn't feel quite right.