Created
March 17, 2010 22:06
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Useful for comparing two objects. | |
/// Example usage: | |
/// var dropOffComparer = new LambdaComparer<ConsolidationDropOff>((x, y) => x.Id == y.Id); | |
/// dropOffs.Contains(dropOff, dropOffComparer); | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
public class LambdaComparer<T> : IEqualityComparer<T> | |
{ | |
Func<T, T, bool> _comparer; | |
Func<T, int> _hash; | |
public LambdaComparer(Func<T, T, bool> comparer) | |
{ | |
_comparer = comparer; | |
_hash = t => t.GetHashCode(); | |
} | |
public LambdaComparer(Func<T, T, bool> comparer, Func<T, int> hash) | |
{ | |
_comparer = comparer; | |
_hash = hash; | |
} | |
public bool Equals(T x, T y) | |
{ | |
return _comparer(x, y); | |
} | |
public int GetHashCode(T obj) | |
{ | |
return _hash(obj); | |
} | |
} | |
public static class SequenceExtensions | |
{ | |
public static bool SequenceEqual<T>(this IEnumerable<T> first, IEnumerable<T> second, Func<T, T, bool> comparer) | |
{ | |
return first.SequenceEqual(second, new LambdaComparer<T>(comparer)); | |
} | |
public static bool SequenceEqual<T>(this IEnumerable<T> first, IEnumerable<T> second, Func<T, T, bool> comparer, Func<T, int> hash) | |
{ | |
return first.SequenceEqual(second, new LambdaComparer<T>(comparer, hash)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment