Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide overload of EntityFrameworkQueryableExtensions.ForEachAsync<T>() that accepts async lambda. #12425

Closed
andrewslavin opened this issue Jun 20, 2018 · 1 comment

Comments

@andrewslavin
Copy link

andrewslavin commented Jun 20, 2018

The issue, as well as the proposed solution, are described here: https://stackoverflow.com/questions/50920636/unexpected-behaviour-with-microsoft-entityframeworkcore-entityframeworkqueryable.

In a nutshell: ForEachAsync() takes Action<T> as second argument, but if I provide Func<T, Task> (async lambda) instead, the code still compiles. But obviously it will not work as expected, because the async lambdas will not be awaited.

@divega
Copy link
Contributor

divega commented Jul 24, 2018

@andrewslavin thank you for making this request here.

We are currently holding off on adding any new functionality to the implementation of async enumerables we use, because we hope to integrate the work on the new async stream interfaces and language support that should be available in an upcoming version.

This is indeed valid feedback you should make sure is either contemplated in the spec of raise an issue in https://github.com/dotnet/csharplang/).

There are also deeper reasons why having an overload of ForEachAsync that takes a Func<TElement,Task> could be a pit of failure. These are detailed in dotnet/reactive#194 (comment) and are related to a high number of unnecessary Task allocations.

Other than that, if in the meanwhile you need something like this, it should be possible for you to create your own extension method to achieve it.

Take a look at how our ForEach extension method obtains the underlying IAsyncEnumerable (note that this requires using EF Core internal API that we will break for sure in EF Core 3.0):

https://github.com/aspnet/EntityFrameworkCore/blob/2d1767e767a8446d3ed5ae6adbc9c55d6c7823d2/src/EFCore/EntityFrameworkQueryableExtensions.cs#L2867-L2871

And then you can implement a modified version of System.Interactive.Async's ForEachAsync that takes a Func<T, Task> and awaits on it on every step of the loop:

https://github.com/dotnet/reactive/blob/7d5254e2d94c5931d58b018c26a90347de160b13/Ix.NET/Source/System.Interactive.Async/ForEach.cs#L76-L89

@divega divega closed this as completed Jul 24, 2018
@ajcvickers ajcvickers removed this from the 2.2.0-preview2 milestone Oct 16, 2018
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants