I have an interface which specifies an IAsyncEnumerable<T>
return value, but for a specific implementation I have the results cached in memory as a List<T>
.
Is there a more straightforward way to return this list which already exists as an IAsyncEnumerable<T>
without having to actually iterate over it? It seems list I shouldn't need the additional overhead of creating an iterator function.
public async IAsyncEnumerable<PartitionModel> GetAllPartitionsAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
{
// _cache.GetOrAddAsync() returns List<PartitionModel>
var list = await _cache.GetOrAddAsync(CacheKey, () => FetchAllPartitionsAsync(cancellationToken), CacheOptions);
foreach (var item in list)
yield return item;
}