我如何“适应"?任务<IEnumerable<T>到 IAsyncEnumerable<T>? [英] How can I "adapt" a Task<IEnumerable<T>> to 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;
}
}
这篇关于我如何“适应"?任务<IEnumerable<T>到 IAsyncEnumerable<T>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!