1
0

For.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class EnumerableEx
  11. {
  12. /// <summary>
  13. /// Generates a sequence by enumerating a source sequence, mapping its elements on result sequences, and concatenating
  14. /// those sequences.
  15. /// </summary>
  16. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  17. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  18. /// <param name="source">Source sequence.</param>
  19. /// <param name="resultSelector">Result selector to evaluate for each iteration over the source.</param>
  20. /// <returns>
  21. /// Sequence concatenating the inner sequences that result from evaluating the result selector on elements from
  22. /// the source.
  23. /// </returns>
  24. public static IEnumerable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. if (resultSelector == null)
  29. throw new ArgumentNullException(nameof(resultSelector));
  30. return ForCore(source, resultSelector)
  31. .Concat();
  32. }
  33. private static IEnumerable<IEnumerable<TResult>> ForCore<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
  34. {
  35. return source.Select(resultSelector);
  36. }
  37. }
  38. }