public class PropertyComparer<T> : IEqualityComparer<T> { private PropertyInfo _PropertyInfo; /// <summary> /// Creates a new instance of PropertyComparer. /// </summary> /// <param name="propertyName">The name of the property on type T /// to perform the comparison on.</param> public PropertyComparer(string propertyName) { //store a reference to the property info object for use during the comparison _PropertyInfo = typeof(T).GetProperty(propertyName, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public); if (_PropertyInfo == null) { throw new ArgumentException(string.Format("{0} is not a property of type {1}.", propertyName, typeof(T))); } } #region IEqualityComparer<T> Members public bool Equals(T x, T y) { //get the current value of the comparison property of x and of y object xValue = _PropertyInfo.GetValue(x, null); object yValue = _PropertyInfo.GetValue(y, null); //if the xValue is null then we consider them equal if and only if yValue is null if (xValue == null) return yValue == null; //use the default comparer for whatever type the comparison property is. return xValue.Equals(yValue); } public int GetHashCode(T obj) { //get the value of the comparison property out of obj object propertyValue = _PropertyInfo.GetValue(obj, null); if (propertyValue == null) return 0; else return propertyValue.GetHashCode(); } #endregion } //Extension Class public static class EnumerableExtender { public static IEnumerable<TSource> Distinct<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { HashSet<TKey> seenKeys = new HashSet<TKey>(); foreach (TSource element in source) { var elementValue = keySelector(element); if (seenKeys.Add(elementValue)) { yield return element; } } } } //using distinctDatas = datas.Distinct(person => person.Name);