Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty IQueryable that implements IAsyncEnumerable

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)
like image 287
Node.JS Avatar asked Oct 30 '25 02:10

Node.JS


1 Answers

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();
}
like image 143
Amir Popovich Avatar answered Nov 01 '25 18:11

Amir Popovich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!