iequalitycomparer

How to get a value from a generic dictionary using a custom comparer for the key?

陌路散爱 提交于 2019-12-25 09:08:15
问题 I have a generic dictionary of objects and want to use a custom comparer to update the value in the dictionary. myObjects contains a dictionary of objects and the value is the number of times that object exists. Note that the value may be incremented numerous times using different comparators or removed altogether. testObject is my custom object. customComparer is a dynamically changing comparer based on the type of testObject. but all comparers are of the type IEqualityComparer<MyObject>

Group linq results by value and group null or invalid values and do counts

寵の児 提交于 2019-12-24 06:37:15
问题 this is a follow on question to this question. I now would like to do some counts on the groupings. Original Query: that excluded invalid zip codes did the following: List<DataSourceRecord> md = (from rst in QBModel.ResultsTable where (!String.IsNullOrWhiteSpace(rst.CallerZipCode) && rst.CallerZipCode.Length > 2) group rst by rst.CallerZipCode.Substring(0, 3) into newGroup orderby newGroup.Key select new DataSourceRecord() { State = newGroup.Select(i => i.CallerState).FirstOrDefault(),

Checking instance of non-class constrained type parameter for null in generic method

三世轮回 提交于 2019-12-23 05:15:31
问题 I currently have a generic method where I want to do some validation on the parameters before working on them. Specifically, if the instance of the type parameter T is a reference type, I want to check to see if it's null and throw an ArgumentNullException if it's null. Something along the lines of: // This can be a method on a generic class, it does not matter. public void DoSomething<T>(T instance) { if (instance == null) throw new ArgumentNullException("instance"); Note, I do not wish to

Dictionary using is custom key but key is always unequal

孤人 提交于 2019-12-23 03:34:06
问题 I am using RTBTextPointer as custom key in dictionary... Init.SpintaxEditorPropertyMain.SpintaxListDict = new Dictionary<RTBTextPointer, SpintaxEditorProperties.SpintaxMappedValue>(new RTBTextPointerComparer()); I worte this RTBTextPointer, and RTBTextPointerComparer classes in class library and using this in different wpf projects, if (Init.SpintaxEditorPropertyMain.SpintaxListDict.ContainsKey(_index) == false) { Init.SpintaxEditorPropertyMain.SpintaxListDict.Add(_index,_SpintaxMappedVal); }

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

回眸只為那壹抹淺笑 提交于 2019-12-21 17:36:19
问题 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

EqualityComparer<T>.Default isn't clever enough

大兔子大兔子 提交于 2019-12-21 11:37:50
问题 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>

Object Comparison in .net

爷,独闯天下 提交于 2019-12-20 04:38:13
问题 Is it any different from the CLR standpoint to implement IEqualityComparer vs overriding the == operator for the property you would use in the IEqualityComparer<T> ? And if so, when would you use one against the other? Edit Ok it does make sense that the IEqaulityComparer used by the implementations of Hashtable - it slipped out of my mind when I was posting the question. So what about the extensions of Linq of IEnumerable. Does that mean that .net builds up a Hashtable when executing those

Equals Remove wrong assignment inside Equals

我们两清 提交于 2019-12-12 03:27:21
问题 I have following class which i am using to compare some objects it looks like it: Imports System.Collections.Generic Public Class Part Implements IEqualityComparer(Of Part) Public _comparisonType As EqualsComparmission Public Sub New(ComparisonType As EqualsComparmission) Me._comparisonType = ComparisonType End Sub Public Sub New() End Sub Public Property PartName() As String Get Return m_PartName End Get Set(value As String) m_PartName = value End Set End Property Private m_PartName As

IEnumerable.Except() and a custom comparer

老子叫甜甜 提交于 2019-12-11 08:12:34
问题 I'm having troubles with the Except() method. Instead of returning the difference, it returns the original set. I've tried implementing the IEquatable and IEqualityComparer in the Account class. I've also tried creating a separate IEqualityComparer class for Account. When the Except() method is called from main, it doesn't seem to call my custom Equals() method, but when I tried Count(), it did call the custom GetHashCode() method! I'm sure I made a trivial mistake somewhere and I hope a

Proper way to write GetHashCode() when Equality Comparer is based on OR operation?

可紊 提交于 2019-12-11 07:39:14
问题 I'm trying to write an Equality Comparer for a simple class with 3 fields, like so: public class NumberClass { public int A { get; set; } public int B { get; set; } public int C { get; set; } } My condition for two objects of NumberClass to be equal is if Obj1.A == Obj2.A || Obj1.B == Obj2.B (in other words, OR), Obj1 and Obj2 being instances of NumberClass . I can easily write the Equals() of my comparer as follows, but I don't know what to do with my GetHashCode() method. public bool Equals