43

When working with an IEnumerable<T> there are the build-in extension methods from the System.Linq namespace such as Skip, Where and Select to work with.

When Microsoft added IAsyncEnumerable in C#8 did they also add new Linq methods to support this?

I could of course implement these methods myself, or maybe find some package which does that, but I'd prefer to use a language-standard method if it exists.

GWigWam
  • 2,013
  • 4
  • 28
  • 34

2 Answers2

54

LINQ for IAsyncEnumerable is supported by System.Linq.Async which is part of the reactive extensions for .NET. The reactive extensions as a whole are split into two larger NuGet packages: System.Reactive and System.Interactive.

While all the packages stayed the same, the extensions now live in the System.Linq namespace, not System.Linq.Async anymore (thanks Dzmitry Lahoda).

Relevant GitHub issue

Joelius
  • 3,839
  • 1
  • 16
  • 36
  • The GitHub issue is closed, does that mean it won't be added to the language, and we'll always have to keep using an external package? – GWigWam Oct 14 '19 at 12:29
  • 6
    It's closed because it's already available in a different package, which is already available [in NuGet](https://www.nuget.org/packages/System.Linq.Async/). Even LINQ is an "external package" - it's available through NuGet, but you don't realize it because it gets downloaded as a dependency of almost every template – Panagiotis Kanavos Oct 14 '19 at 12:31
  • 8
    Update: `System.Linq.Async` appears to be a separate package now: https://www.nuget.org/packages/System.Linq.Async – Benno Straub Oct 06 '21 at 12:33
  • 1
    It appears that it's the extension `FirstOrDefaultAsync()`, which takes the expected `CancellationToken` – Steve Friedl Mar 28 '22 at 16:06
  • 1
    Also note that the return type of `FirstOrDefaultAsync()` is `ValueTask`, not `Task` – Steve Friedl Mar 28 '22 at 16:16
  • For anybody getting confused about all the packages mentioned: You want the NuGet Package [`System.Linq.Async`](https://www.nuget.org/packages/System.Linq.Async/) (not [`System.Interactive`](https://www.nuget.org/packages/System.Interactive/)). The package exposes its async Linq extensions in the namespace `System.Linq`. – 3dGrabber Apr 23 '23 at 16:21
0

Since .NET 7, you can use .ToBlockingEnumerable() to convert an IAsyncEnumerable<T> to an IEnumerable<T>, which you can then call your LINQ methods off of.

UnionP
  • 1,251
  • 13
  • 26
  • The question was about Linq support to `IAsyncEnumerable`. Your answer degrade the async collection to a blocking sync cursor which isn’t the best approach to async programming. – Lord of the Goo Jun 03 '23 at 23:33
  • That's fair, depending on which LINQ method someone's trying to call that may be necessary (for example, Sum, Count, ToList, etc. But it's a good thing to caveat. I'm glad the method name has "Blocking" in it! – UnionP Jun 05 '23 at 00:12
  • To anyone who considers using this, be very sure that you understand what "blocking" means. It's the last way out of a stick situation that should not be taken if it's avoidable. – Chris Jensen Aug 09 '23 at 07:22