iequalitycomparer

Best way to compare two Dictionary<T> for equality

余生长醉 提交于 2019-12-05 09:03:04
Is this the best way to create a comparer for the equality of two dictionaries? This needs to be exact. Note that Entity.Columns is a dictionary of KeyValuePair(string, object) : public class EntityColumnCompare : IEqualityComparer<Entity> { public bool Equals(Entity a, Entity b) { var aCol = a.Columns.OrderBy(KeyValuePair => KeyValuePair.Key); var bCol = b.Columns.OrderBy(KeyValuePAir => KeyValuePAir.Key); if (aCol.SequenceEqual(bCol)) return true; else return false; } public int GetHashCode(Entity obj) { return obj.Columns.GetHashCode(); } } Also not too sure about the GetHashCode

Implementing IEqualityComparer<T> for comparing arbitrary properties of any class (including anonymous)

时间秒杀一切 提交于 2019-12-04 10:39:47
I am writing this neat class implementing IEqualityComparer, so that I can just pass any anonymous type to it (or actually any type with properties) and it will automatically compare the types by comparing the property values of the types. public class CompareProperty<T> : IEqualityComparer<T> { private Type type; private PropertyInfo propInfo; private string _fieldName; public string fieldName { get; set; } public CompareProperty(string fieldName) { this.fieldName = fieldName; } public bool Equals<T>(T x, T y) { if (this.type == null) { type = x.GetType(); propInfo = type.GetProperty

Using an IEqualityComparer with a LINQ to Entities Except clause

久未见 提交于 2019-12-04 08:50:06
I have an entity that I'd like to compare with a subset and determine to select all except the subset. So, my query looks like this: Products.Except(ProductsToRemove(), new ProductComparer()) The ProductsToRemove() method returns a List<Product> after it performs a few tasks. So in it's simplest form it's the above. The ProductComparer() class looks like this: public class ProductComparer : IEqualityComparer<Product> { public bool Equals(Product a, Product b) { if (ReferenceEquals(a, b)) return true; if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false; return a.Id == b.Id; }

Why we need the IEqualityComparer,IEqualityComparer<T> interface?

删除回忆录丶 提交于 2019-12-04 07:09:41
the 'Equal' and 'GetHashcode' method are exist in the object class, and our type inherit the object base class. what's the different between implement the two methods of the object directly and using the IComparer interface? if we overriding object's Equal and GetHashCode , and push to a hashtable , it will use the overring 's equal method? what' the differents of new a hashtable with the IEqualityComparer constructor? The IComparable interface is used when you need to be able to "sort" objects, and it gives you a method ( CompareTo ) that tells you if two objects are <, = or > . The

EqualityComparer<T>.Default isn't clever enough

不想你离开。 提交于 2019-12-04 05:01:13
I was reading the source code of EqualityComparer<T>.Default and found that it's not so clever. Here is an example: enum MyEnum : int { A, B } EqualityComparer<MyEnum>.Default.Equals(MyEnum.A, MyEnum.B) //is as fast as EqualityComparer<int>.Default.Equals(0, 1) enum AnotherEnum : long { A = 1L, B = 2L } //is 8x slower than EqualityComparer<long>.Default.Equals(1L, 2L) The reason is obvious from the source code of the private method in EqualityComparer. private static EqualityComparer<T> CreateComparer() { //non-important codes are ignored if (c.IsEnum && (Enum.GetUnderlyingType(c) == typeof

Implementing IEqualityComparer<T> on an object with two properties in C#

与世无争的帅哥 提交于 2019-12-03 10:20:00
问题 I have a case where I need to grab a bunch of items on distinct, but my source is a collection of objects with two properties, like this: public class SkillRequirement { public string Skill { get; set; } public string Requirement { get; set; } } I try to get a collection as follows: SkillRequirementComparer sCom = new SkillRequirementComparer(); var distinct_list = source.Distinct(sCom); I tried to implement an IEqualityComparer<T> for this, but I fell stumped at the GetHashCode() method. The

EqualityComparer<T>.Default vs. T.Equals

…衆ロ難τιáo~ 提交于 2019-12-03 05:33:43
问题 Suppose I've got a generic MyClass<T> that needs to compare two objects of type <T> . Usually I'd do something like ... void DoSomething(T o1, T o2) { if(o1.Equals(o2)) { ... } } Now suppose my MyClass<T> has a constructor that supports passing a custom IEqualityComparer<T> , similar to Dictionary<T>. In that case I'd need to do ... private IEqualityComparer<T> _comparer; public MyClass() {} public MyClass(IEqualityComparer<T> comparer) { _comparer = comparer; } void DoSomething(T o1, T o2) {

EqualityComparer<T>.Default vs. T.Equals

会有一股神秘感。 提交于 2019-12-02 20:08:10
Suppose I've got a generic MyClass<T> that needs to compare two objects of type <T> . Usually I'd do something like ... void DoSomething(T o1, T o2) { if(o1.Equals(o2)) { ... } } Now suppose my MyClass<T> has a constructor that supports passing a custom IEqualityComparer<T> , similar to Dictionary<T> . In that case I'd need to do ... private IEqualityComparer<T> _comparer; public MyClass() {} public MyClass(IEqualityComparer<T> comparer) { _comparer = comparer; } void DoSomething(T o1, T o2) { if((_comparer != null && _comparer.Equals(o1, o2)) || (o1.Equals(o2))) { ... } } To remove this

Checking equality with a HashSet of objects

流过昼夜 提交于 2019-12-02 11:40:30
问题 I am trying to compare two hashsets of Definition type as EqualityComparer<T>.Default.Equals(value, oldValue) . Definition is defined as follows public class Definition { public string Variable { get; set; } public HashSet<Location> LocationList { get; set; } public override bool Equals(object obj) { Definition other = obj as Definition; return other.Variable.Equals(this.Variable) && other.LocationList!= null &&this.LocationList != null && other.LocationList.Count == this.LocationList.Count &

Custom equality comparer for Type in dictionary

纵然是瞬间 提交于 2019-12-02 02:25:38
问题 Since Int32 is a Object , I want this to print "True" Dictionary<Type, string> dict = new Dictionary<Type, string>(new MyComparer()); dict[typeof(object)] = "Hello"; Console.WriteLine(dict.ContainsKey(typeof(int))); // currently prints false :( Here's the comparer I tried: public class MyComparer : IEqualityComparer<Type> { public bool Equals(Type x, Type y) { return y.IsAssignableFrom(x); } public int GetHashCode(Type obj) { return obj.GetHashCode(); } } But it's not working. I'm not quite