LINQ Interview Question #146

Does LINQ to Objects automatically become asynchronous when using async/await?

Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced

Quick Interview Answer

No. Standard IEnumerable<T> LINQ operators are synchronous. Simply placing them inside an async method does not make CPU-bound enumeration asynchronous.

Detailed Explanation

No. Standard IEnumerable<T> LINQ operators are synchronous. Simply placing them inside an async method does not make CPU-bound enumeration asynchronous.

Asynchronous streams use IAsyncEnumerable<T> and await foreach. Database frameworks can also provide their own asynchronous query execution methods because the underlying operation involves asynchronous I/O.

Code Example

await foreach (var item in GetItemsAsync())
{
    Console.WriteLine(item.Name);
}

// Ordinary IEnumerable<T>.Where(...) remains synchronous.