Lidarr/src/NzbDrone.Common/Extensions/IEnumerableExtensions.cs

46 lines
1.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
namespace NzbDrone.Common.Extensions
{
2013-09-19 01:09:26 +00:00
public static class EnumerableExtensions
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var knownKeys = new HashSet<TKey>();
return source.Where(element => knownKeys.Add(keySelector(element)));
}
2014-04-30 23:39:54 +00:00
public static void AddIfNotNull<TSource>(this List<TSource> source, TSource item)
{
if (item == null)
{
return;
}
source.Add(item);
}
public static bool Empty<TSource>(this IEnumerable<TSource> source)
{
return !source.Any();
}
public static bool None<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
return !source.Any(predicate);
}
public static bool NotAll<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
return !source.All(predicate);
}
public static List<TResult> SelectList<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> predicate)
{
return source.Select(predicate).ToList();
}
}
}