jellyfin/RSSDP/IEnumerableExtensions.cs

35 lines
950 B
C#
Raw Normal View History

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