Skip to content

Understanding EFCore's IAsyncEnumerableΒ #33338

@Mike-E-angelo

Description

@Mike-E-angelo

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions