IEnumerableExtensions.cs 950 B

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Rssdp.Infrastructure
  5. {
  6. internal static class IEnumerableExtensions
  7. {
  8. public static IEnumerable<T> SelectManyRecursive<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
  9. {
  10. if (source == null)
  11. {
  12. throw new ArgumentNullException(nameof(source));
  13. }
  14. if (selector == null)
  15. {
  16. throw new ArgumentNullException(nameof(selector));
  17. }
  18. return !source.Any() ? source :
  19. source.Concat(
  20. source
  21. .SelectMany(i => selector(i).EmptyIfNull())
  22. .SelectManyRecursive(selector)
  23. );
  24. }
  25. public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
  26. {
  27. return source ?? Enumerable.Empty<T>();
  28. }
  29. }
  30. }