LINQ's answer was posted earlier. I copy from Richard Salai the answer here: Filtering duplicates from IEnumerable
public static class EnumerationExtensions { public static IEnumerable<TSource> Distinct<TSource,TKey>( this IEnumerable<TSource> source, Func<TSource,TKey> keySelector) { KeyComparer comparer = new KeyComparer(keySelector); return source.Distinct(comparer); } private class KeyComparer<TSource,TKey> : IEqualityComparer<TSource> { private Func<TSource,TKey> keySelector; public DelegatedComparer(Func<TSource,TKey> keySelector) { this.keySelector = keySelector; } bool IEqualityComparer.Equals(TSource a, TSource b) { if (a == null && b == null) return true; if (a == null || b == null) return false; return keySelector(a) == keySelector(b); } int IEqualityComparer.GetHashCode(TSource obj) { return keySelector(obj).GetHashCode(); } } }
Which, as Richard says, is used as follows:
var distinct = arr.Distinct(x => x.Name);
source share