-
-
Save DCCoder90/d358ace7ef36401dd6f0449d4ab87706 to your computer and use it in GitHub Desktop.
public static class AsyncEnumerableExtensions | |
{ | |
public static IAsyncEnumerable<TResult> SelectAsync<T, TResult>(this IEnumerable<T> enumerable, | |
Func<T, Task<TResult>> selector) | |
{ | |
return AsyncEnumerable.CreateEnumerable(() => | |
{ | |
var enumerator = enumerable.GetEnumerator(); | |
var current = default(TResult); | |
return AsyncEnumerable.CreateEnumerator(async c => | |
{ | |
var moveNext = enumerator.MoveNext(); | |
current = moveNext | |
? await selector(enumerator.Current).ConfigureAwait(false) | |
: default(TResult); | |
return moveNext; | |
}, | |
() => current, | |
() => enumerator.Dispose()); | |
}); | |
} | |
} |
But in AsyncEnumerable
in System.Linq.Async
there already is a ToAsyncEnumerable
function.
https://github.com/dotnet/reactive/blob/main/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToAsyncEnumerable.cs
Well, glad to know I was on the right track. If that's the case, why did this gist come up when I Googled for this?
Well, glad to know I was on the right track. If that's the case, why did this gist come up when I Googled for this?
AFAIK Google doesn't index GitHub repos directly because source code changes too frequently, but it does index Gists which are relatively static.
Consider updating the git description with a "TL&DR: use System.Linq.Async's ToAsyncEnumerable()"
Personally I thought it'd be called AsAsyncEnumerable() and turned to Google after that didn't exist.
You can do it like this with
System.Linq.Async