---
title: "Discussion on \"LINQ: Beware of deferred execution\" | Hashnode"
description: "Discussion on \"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.\nThe example below demonstrate a ..."
url: "https://hashnode.com/posts/linq-beware-of-deferred-execution/60853ce5e1555b2a98aaaf6d"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [LINQ: Beware of deferred execution](https://samwalpole.com/linq-beware-of-deferred-execution?utm_source=hashnode&utm_medium=feed)

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 ...

Apr 25, 2021 · 6 min read · samwalpole.com  
Updated Aug 25, 2024  
Read the article

-   [#csharp](https://hashnode.com/tag/csharp)
-   [#dotnet](https://hashnode.com/tag/dotnet)
-   [#dotnetcore](https://hashnode.com/tag/dotnetcore)
-   [#tutorial](https://hashnode.com/tag/tutorial)

## Written by

[Sam Walpole](https://hashnode.com/@dr_sam_walpole)

@dr\_sam\_walpole · United Kingdom

  
[View profile](https://hashnode.com/@dr_sam_walpole)

👨‍💻 Fullstack .NET and JavaScript teacher and advocate 👨‍💻

## Responses 1

Markdown supported · ⌘↵ to post

[Ewald Bayer](https://hashnode.com/@E4est)

[Aug 26, 2021](https://hashnode.com/posts/linq-beware-of-deferred-execution/60853ce5e1555b2a98aaaf6d/comment/6127657471cb04254f06cff5)

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.

[Sam Walpole](https://hashnode.com/@dr_sam_walpole)

[Aug 26, 2021](https://hashnode.com/posts/linq-beware-of-deferred-execution/60853ce5e1555b2a98aaaf6d/comment/6127657471cb04254f06cff5#reply-6127742971cb04254f06d188)

Thank you for your comment. You are correct. This would be a better solution. I have updated the article to reflect this. Many thanks.
