For.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Generates a sequence by enumerating a source sequence, mapping its elements on result sequences, and concatenating
  11. /// those sequences.
  12. /// </summary>
  13. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  14. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  15. /// <param name="source">Source sequence.</param>
  16. /// <param name="resultSelector">Result selector to evaluate for each iteration over the source.</param>
  17. /// <returns>
  18. /// Sequence concatenating the inner sequences that result from evaluating the result selector on elements from
  19. /// the source.
  20. /// </returns>
  21. public static IEnumerable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
  22. {
  23. if (source == null)
  24. throw new ArgumentNullException(nameof(source));
  25. if (resultSelector == null)
  26. throw new ArgumentNullException(nameof(resultSelector));
  27. return ForCore(source, resultSelector)
  28. .Concat();
  29. }
  30. private static IEnumerable<IEnumerable<TResult>> ForCore<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
  31. {
  32. return source.Select(resultSelector);
  33. }
  34. }
  35. }