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

Understanding EFCore's IAsyncEnumerable #33338

Open
Mike-E-angelo opened this issue Mar 16, 2024 · 1 comment
Open

Understanding EFCore's IAsyncEnumerable #33338

Mike-E-angelo opened this issue Mar 16, 2024 · 1 comment

Comments

@Mike-E-angelo
Copy link

Ask a question

I am currently experiencing an issue with calling EF.CompileAsyncQuery(Expression<Func<DbContext, IQueryable<TOut>>>) and iterating through the IAsyncEnumerable<T> that is returned from it.

I am using "Microsoft.EntityFrameworkCore": "Information" to retrieve the query being sent to SQL Server.

In this case, the query I am using returns over 1500 rows, which, if I take the query emitted to the console and paste it into SSMS, takes about 16 seconds to process it.

The problem I am experiencing is that if I run the following by passing in the IAsyncEnumerable created from EF.CompileAsyncQuery to the Get method below:

sealed class ToFirstOrDefault<T> : IEvaluate<T, T?>
{
	public static ToFirstOrDefault<T> Default { get; } = new();

	ToFirstOrDefault() {}

	public async ValueTask<T?> Get(IAsyncEnumerable<T> parameter)
	{
		await foreach (var item in parameter.ConfigureAwait(false))
		{
			return item;
		}

		return default;
	}
}

... it also takes 16 seconds to execute, which is not expected. My understanding/expectation that when the EFCore IAsyncEnumerable is enumerated it opens a cursor on the server and processes each row one at a time each time the MoveNextAsync is called.

Is this understanding correct? If this is, I would not expect the above to take 16 seconds to process as there should only be one row being processed.

I would greatly appreciate any pointers to assist me in this area. Thank you for any assistance you can provide.

Include provider and version information

EF Core version: 8.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: (e.g. .NET 8.0)
Operating system: Windows 10
IDE: Visual Studio 2022 17.10 Preview 2

@Mike-E-angelo
Copy link
Author

Thank you for any clarification you can provide on this issue.

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