我如何“适应"?任务&lt;IEnumerable&lt;T&gt;到 IAsyncEnumerable<T>? [英] How can I &quot;adapt&quot; a Task&lt;IEnumerable&lt;T&gt;&gt; to IAsyncEnumerable&lt;T&gt;?

查看:35
本文介绍了我如何“适应"?任务&lt;IEnumerable&lt;T&gt;到 IAsyncEnumerable<T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在逐步将 Ix.NET 引入到遗留项目中.我有许多存储级 API 返回 Task>,但我想将它们调整为 IAsyncEnumerable 以便在其余部分使用系统的.似乎应该有一个辅助方法(ala .ToAsyncEnumerable() for IEnumerable)来帮助解决这个问题,但我找不到任何东西......我必须实现我自己的自定义枚举器吗?(不难,但我不想重新发明轮子)

I'm incrementally introducing Ix.NET into a legacy project. I have a number of storage-level APIs that return Task<IEnumerable<T>>, but I want to adapt them to IAsyncEnumerable<T> for consumption in the rest of the system. It seems like there should be a helper method (ala .ToAsyncEnumerable() for IEnumerable) to help with this, but I can't find anything... Do I have to implement my own custom Enumerator? (not hard, but I don't want to re-invent the wheel)

推荐答案

Task<IEnumerable<T>> GetSomeResults<T>()
{
    throw new NotImplementedException();
}

async IAsyncEnumerable<T> GetAsyncEnumerable<T>()
{
    var results = await GetSomeResults<T>();
    foreach(var item in results)
    {
        yield return item;
    }
}

这篇关于我如何“适应"?任务&lt;IEnumerable&lt;T&gt;到 IAsyncEnumerable<T>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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