I am looking for an empty IQueryable<T> that implements IAsyncEnumerable<T>. My current code does not work because empty Enumerable does not implement IAsyncEnumerable<T>. Thanks for any help or hint.
I have the following design:
var result = Enumerable.Empty<Foo>().AsQueryable(); // Not working!
if (condition1)
{
IQueryable<Foo> part1 = ....;
result = result.Concat(part1);
}
if (condition2)
{
IQueryable<Foo> part2 = ....;
result = result.Concat(part2);
}
return await result.ToListAsync();
Error message:
The source IQueryable doesn't implement IAsyncEnumerable<Foo>. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations.
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsAsyncEnumerable[TSource](IQueryable`1 source)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
Use the nuget package System.Linq.Async to get the ToAsyncEnumerable() method:
private static async Task<List<Foo>> GetList()
{
var result = Enumerable.Empty<Foo>().AsQueryable();
if (true)
{
IQueryable<Foo> part1 = new List<Foo> { new Foo() }.AsQueryable();
result = result.Concat(part1);
}
if (true)
{
IQueryable<Foo> part2 = new List<Foo> { new Foo(), new Foo() }.AsQueryable();
result = result.Concat(part2);
}
return await result.ToAsyncEnumerable().ToListAsync();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With