IEqualityComparer for anonymous type

The trick is to create a comparer that only works on inferred types. For instance: public class Comparer<T> : IComparer<T> { private Func<T,T,int> _func; public Comparer(Func<T,T,int> func) { _func = func; } public int Compare(T x, T y ) { return _func(x,y); } } public static class Comparer { public static Comparer<T> Create<T>(Func<T,T,int> func){ return … Read more

EqualityComparer.Default vs. T.Equals

It should be the same, but it is not guaranteed, because it depends on implementation details of the type T. Explanation: Without a constraint to T, o1.Equals(o2) will call Object.Equals, even if T implements IEquatable<T>. EqualityComparer<T>.Default however, will use Object.Equals only, if T doesn’t implement IEquatable<T>. If it does implement that interface, it uses IEquatable<T>.Equals. … Read more

What problem does IStructuralEquatable and IStructuralComparable solve?

All types in .NET support the Object.Equals() method which, by default, compares two types for reference equality. However, sometimes, it also desirable to be able to compare two types for structural equality. The best example of this is arrays, which with .NET 4 now implement the IStructuralEquatable interface. This makes it possible to distinguish whether … Read more

IEqualityComparer that uses ReferenceEquals

Just in case there is no default implementation, this is my own: Edit by 280Z28: Rationale for using RuntimeHelpers.GetHashCode(object), which many of you probably haven’t seen before. 🙂 This method has two effects that make it the correct call for this implementation: It returns 0 when the object is null. Since ReferenceEquals works for null … Read more

How to implement IEqualityComparer to return distinct values?

An EqualityComparer is not the way to go – it can only filter your result set in memory eg: var objects = yourResults.ToEnumerable().Distinct(yourEqualityComparer); You can use the GroupBy method to group by IDs and the First method to let your database only retrieve a unique entry per ID eg: var objects = yourResults.GroupBy(o => o.Id).Select(g … Read more

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

I’m not much sure what useful it really is, as I think for most cases in the Base Library expecting an IComparer there’s an overload that expects a Comparison… but just for the record: in .Net 4.5 they’ve added a method to obtain an IComparer from a Comparison: Comparer.Create so you can pass your lambda … Read more

How to use the IEqualityComparer

Your GetHashCode implementation always returns the same value. Distinct relies on a good hash function to work efficiently because it internally builds a hash table. When implementing interfaces of classes it is important to read the documentation, to know which contract you’re supposed to implement.1 In your code, the solution is to forward GetHashCode to … Read more

What’s the role of GetHashCode in the IEqualityComparer in .NET?

A bit of background first… Every object in .NET has an Equals method and a GetHashCode method. The Equals method is used to compare one object with another object – to see if the two objects are equivalent. The GetHashCode method generates a 32-bit integer representation of the object. Since there is no limit to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)