gpt4 book ai didi

c# - 实现 IAsyncEnumerable 的空 IQueryable

转载 作者:行者123 更新时间:2023-12-02 01:19:40 38 4
gpt4 key购买 nike

我正在寻找一个空的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/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com