jellyfin-server/RSSDP/IEnumerableExtensions.cs

29 lines
847 B
C#
Raw Normal View History

2016-10-29 22:22:20 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rssdp.Infrastructure
{
internal static class IEnumerableExtensions
{
public static IEnumerable<T> SelectManyRecursive<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (selector == null) throw new ArgumentNullException(nameof(selector));
2016-10-29 22:22:20 +00:00
2019-01-07 23:24:34 +00:00
return !source.Any() ? source :
source.Concat(
source
.SelectMany(i => selector(i).EmptyIfNull())
.SelectManyRecursive(selector)
);
}
2016-10-29 22:22:20 +00:00
2019-01-07 23:24:34 +00:00
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
{
return source ?? Enumerable.Empty<T>();
}
}
2016-10-29 22:22:20 +00:00
}