实现 IAsyncEnumerable 的空 IQueryable

Nod*_*.JS 3 c# entity-framework iqueryable

我正在寻找一个IQueryable<T>实现IAsyncEnumerable<T>. 我当前的代码不起作用,因为空Enumerable没有实现IAsyncEnumerable<T>。感谢您的任何帮助或提示。

我有以下设计:

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();
Run Code Online (Sandbox Code Playgroud)

错误信息:

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)
Run Code Online (Sandbox Code Playgroud)

Ami*_*ich 5

使用nuget包System.Linq.Async获取ToAsyncEnumerable()方法:

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();
}
Run Code Online (Sandbox Code Playgroud)