实现IAsyncEnumerable的空IQueryable [英] Empty IQueryable that implements IAsyncEnumerable

查看:108
本文介绍了实现IAsyncEnumerable的空IQueryable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找实现 IAsyncEnumerable< T> 的空 IQueryable< T> .我当前的代码不起作用,因为空的 Enumerable 无法实现 IAsyncEnumerable< T> .感谢您的帮助或提示.

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.

我的设计如下:

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()方法:

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();
}

这篇关于实现IAsyncEnumerable的空IQueryable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆