Extension methods for finding distinct items based on a predicate

This commit is contained in:
Chris Cameron
2018-02-21 12:17:57 -05:00
parent cd4791538a
commit 24d674337b
4 changed files with 78 additions and 45 deletions

View File

@@ -653,23 +653,51 @@ namespace ICD.Common.Utils.Extensions
}
/// <summary>
/// Gets distinct elements from the sequence based on given callbacks.
/// Gets distinct elements from the sequence based on given property.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TItem"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="extends"></param>
/// <param name="comparer"></param>
/// <param name="getHashCode"></param>
/// <param name="getProperty"></param>
/// <returns></returns>
[PublicAPI]
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> extends, Func<T, T, bool> comparer, Func<T, int> getHashCode)
public static IEnumerable<TItem> Distinct<TItem, TProperty>(this IEnumerable<TItem> extends,
Func<TItem, TProperty> getProperty)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (comparer == null)
throw new ArgumentNullException("comparer");
if (getProperty == null)
throw new ArgumentNullException("getProperty");
return extends.Distinct(new FuncComparer<T>(comparer, getHashCode));
IEqualityComparer<TProperty> comparer = EqualityComparer<TProperty>.Default;
return extends.Distinct(getProperty, comparer);
}
/// <summary>
/// Gets distinct elements from the sequence based on given property.
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="extends"></param>
/// <param name="getProperty"></param>
/// <param name="propertyComparer"></param>
/// <returns></returns>
[PublicAPI]
public static IEnumerable<TItem> Distinct<TItem, TProperty>(this IEnumerable<TItem> extends,
Func<TItem, TProperty> getProperty,
IEqualityComparer<TProperty> propertyComparer)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (getProperty == null)
throw new ArgumentNullException("getProperty");
if (propertyComparer == null)
throw new ArgumentNullException("propertyComparer");
return extends.Distinct(new PropertyEqualityComparer<TItem, TProperty>(propertyComparer, getProperty));
}
/// <summary>