/// <summary> /// This extension method will enumerate this <see cref="IAsyncEnumerable{T}"/> into a <see cref="List{T}"/>. /// </summary> /// <typeparam name="T">The type of items.</typeparam> /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> /// <returns>A <see cref="List{T}"/> of enumerated items.</returns> /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> public static async Task <List <T> > ToListAsync <T>(this IAsyncEnumerable <T> enumerable) { ArgumentValidator.ValidateNotNullReference(enumerable); var retVal = new List <T>(); await enumerable.AddToCollectionAsync(retVal, (list, item) => list.Add(item)); return(retVal); }
/// <summary> /// This extension method will enumerate this <see cref="IAsyncEnumerable{T}"/> into a <see cref="IDictionary{TKey, TValue}"/>. /// </summary> /// <typeparam name="T">The type of items being enumerated.</typeparam> /// <typeparam name="TKey">The type of dictionary keys.</typeparam> /// <typeparam name="TValue">The type of dictionary values.</typeparam> /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> /// <param name="keySelector">The callback to potentially asynchronously create a dictionary key from enumerable item.</param> /// <param name="valueSelector">The callback to potentially asynchronously create a dictionary value from enumerable item.</param> /// <param name="equalityComparer">The optional <see cref="IEqualityComparer{T}"/> to use when creating dictionary.</param> /// <returns>Asynchronously returns a <see cref="IDictionary{TKey, TValue}"/> containing keys and values as returned by <paramref name="keySelector"/> and <paramref name="valueSelector"/>.</returns> /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If either of <paramref name="keySelector"/> or <paramref name="valueSelector"/> is <c>null</c>.</exception> public static async Task <IDictionary <TKey, TValue> > ToDictionaryAsync <T, TKey, TValue>( this IAsyncEnumerable <T> enumerable, Func <T, ValueTask <TKey> > keySelector, Func <T, ValueTask <TValue> > valueSelector, IEqualityComparer <TKey> equalityComparer = null ) { ArgumentValidator.ValidateNotNullReference(enumerable); ArgumentValidator.ValidateNotNull(nameof(keySelector), keySelector); ArgumentValidator.ValidateNotNull(nameof(valueSelector), valueSelector); var retVal = new Dictionary <TKey, TValue>(equalityComparer); await enumerable.AddToCollectionAsync(retVal, async ( dictionary, item ) => dictionary.Add(await keySelector(item), await valueSelector(item))); return(retVal); }