Skip to content

Instantly share code, notes, and snippets.

@kpko
Last active August 29, 2015 14:21
Show Gist options
  • Save kpko/7e64cb14287bcf4197b9 to your computer and use it in GitHub Desktop.
Save kpko/7e64cb14287bcf4197b9 to your computer and use it in GitHub Desktop.
.NET: ProjectionEqualityComparer<T>
public class ProjectionEqualityComparer<T> : IEqualityComparer<T>
{
Func<T, object> _projector;
public ProjectionEqualityComparer(Func<T, object> projector)
{
this._projector = projector;
}
public bool Equals(T x, T y)
{
if (x == null || y == null) return (x == null && y == null);
return object.Equals(_projector(x), _projector(y));
}
public int GetHashCode(T obj)
{
return _projector(obj).GetHashCode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment