2013-08-22 04:42:25 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2017-02-11 06:46:39 +00:00
|
|
|
using System.Linq.Expressions;
|
2013-08-22 04:42:25 +00:00
|
|
|
|
2014-12-02 06:26:25 +00:00
|
|
|
namespace NzbDrone.Common.Extensions
|
2013-08-22 04:42:25 +00:00
|
|
|
{
|
2013-09-19 01:09:26 +00:00
|
|
|
public static class EnumerableExtensions
|
2013-08-22 04:42:25 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-12-25 09:22:00 +00:00
|
|
|
public static IEnumerable<TFirst> IntersectBy<TFirst, TSecond, TKey>(this IEnumerable<TFirst> first, Func<TFirst, TKey> firstKeySelector,
|
|
|
|
IEnumerable<TSecond> second, Func<TSecond, TKey> secondKeySelector,
|
|
|
|
IEqualityComparer<TKey> keyComparer)
|
|
|
|
{
|
|
|
|
var keys = new HashSet<TKey>(second.Select(secondKeySelector), keyComparer);
|
|
|
|
|
|
|
|
foreach (var element in first)
|
|
|
|
{
|
|
|
|
var key = firstKeySelector(element);
|
|
|
|
|
|
|
|
// Remove the key so we only yield once
|
|
|
|
if (keys.Remove(key))
|
|
|
|
{
|
|
|
|
yield return element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IEnumerable<TFirst> ExceptBy<TFirst, TSecond, TKey>(this IEnumerable<TFirst> first, Func<TFirst, TKey> firstKeySelector,
|
|
|
|
IEnumerable<TSecond> second, Func<TSecond, TKey> secondKeySelector,
|
|
|
|
IEqualityComparer<TKey> keyComparer)
|
|
|
|
{
|
|
|
|
var keys = new HashSet<TKey>(second.Select(secondKeySelector), keyComparer);
|
|
|
|
var matchedKeys = new HashSet<TKey>();
|
|
|
|
|
|
|
|
foreach (var element in first)
|
|
|
|
{
|
|
|
|
var key = firstKeySelector(element);
|
|
|
|
|
|
|
|
if (!keys.Contains(key) && !matchedKeys.Contains(key))
|
|
|
|
{
|
|
|
|
// Store the key so we only yield once
|
|
|
|
matchedKeys.Add(key);
|
|
|
|
yield return element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:22:10 +00:00
|
|
|
public static Dictionary<TKey, TItem> ToDictionaryIgnoreDuplicates<TItem, TKey>(this IEnumerable<TItem> src, Func<TItem, TKey> keySelector)
|
|
|
|
{
|
|
|
|
var result = new Dictionary<TKey, TItem>();
|
|
|
|
foreach (var item in src)
|
|
|
|
{
|
|
|
|
var key = keySelector(item);
|
|
|
|
if (!result.ContainsKey(key))
|
|
|
|
{
|
|
|
|
result[key] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Dictionary<TKey, TValue> ToDictionaryIgnoreDuplicates<TItem, TKey, TValue>(this IEnumerable<TItem> src, Func<TItem, TKey> keySelector, Func<TItem, TValue> valueSelector)
|
|
|
|
{
|
|
|
|
var result = new Dictionary<TKey, TValue>();
|
|
|
|
foreach (var item in src)
|
|
|
|
{
|
|
|
|
var key = keySelector(item);
|
|
|
|
if (!result.ContainsKey(key))
|
|
|
|
{
|
|
|
|
result[key] = valueSelector(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2014-05-19 19:14:41 +00:00
|
|
|
|
|
|
|
public static bool Empty<TSource>(this IEnumerable<TSource> source)
|
|
|
|
{
|
|
|
|
return !source.Any();
|
|
|
|
}
|
2014-10-28 04:37:35 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-05-05 14:29:38 +00:00
|
|
|
|
|
|
|
public static List<TResult> SelectList<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> predicate)
|
|
|
|
{
|
|
|
|
return source.Select(predicate).ToList();
|
|
|
|
}
|
2017-02-11 06:46:39 +00:00
|
|
|
|
|
|
|
// public static IOrderedEnumerable<TEntity> OrderBy<TEntity>(this IEnumerable<TEntity> source, string propertyName, bool descending)
|
|
|
|
// {
|
|
|
|
// var property = typeof(TEntity).GetProperty(propertyName);
|
|
|
|
// Func<TEntity, Object> orderByFunc = x => property.GetValue(x, null);
|
|
|
|
//
|
|
|
|
// return descending ? source.OrderByDescending(orderByFunc) : source.OrderBy(orderByFunc);
|
|
|
|
// }
|
2013-08-22 04:42:25 +00:00
|
|
|
}
|
2018-03-18 15:22:10 +00:00
|
|
|
}
|