Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

542
Views
How can I run a async enumerator method synchronously and store it as an IEnumerable?

I have an async method that returns an IAsyncEnumerable using yield. In some cases, it may be needed to get the items synchronously so I want to make another method for this that returns IEnumerable by running the existing method synchronously in order to avoid duplicate code.

async IAsyncEnumerable<Foo> GetItemsAsync()
{
    yield return ...
}

IEnumerable<Foo> GetItems() => GetItemsAsync() // Run shyncronously

I read plenty solutions about running async methods synchronously using techniques like the Task.RunSynchronously or simply calling it in a sync context and ignoring the warning. However, it seems neither of these is possible in the case of async enumerables. The interface features no RunSynchronously method and trying to enumerate through it using a foreach that is not awaited causes a compilation error. Getting the enumerator and running MoveNext synchronously also doesn't seem to be an option since that method returns ValueTask which doesn't feature Wait methods and I need to capture the result of the task to know when to stop iterating.

So is it possible to force my async enomerator method synchronously and get the result as IEnumerable, and if so, how?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could use the ToEnumerable extension method from the System.Linq.Async package. The signature of this method is:

public static IEnumerable<TSource> ToEnumerable<TSource>(
    this IAsyncEnumerable<TSource> source)

...and you could use it like this:

IEnumerable<Foo> GetItems() => GetItemsAsync().ToEnumerable();

Update: an extension method ToBlockingEnumerable with identical functionality might be included in the next version of the .NET platform (.NET 7). Related GitHub issue: Add an IAsyncEnumerable.ToEnumerable extension method.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!