IEnumerableExtensions.cs 861 B

123456789101112131415161718192021222324252627
  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) throw new ArgumentNullException(nameof(source));
  11. if (selector == null) throw new ArgumentNullException(nameof(selector));
  12. return !source.Any() ? source :
  13. source.Concat(
  14. source
  15. .SelectMany(i => selector(i).EmptyIfNull())
  16. .SelectManyRecursive(selector)
  17. );
  18. }
  19. public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
  20. {
  21. return source ?? Enumerable.Empty<T>();
  22. }
  23. }
  24. }