我正在寻找一个空的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();
错误消息:
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)
最佳答案
使用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();
}
关于c# - 实现 IAsyncEnumerable 的空 IQueryable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60274758/