Questions tagged [iequalitycomparer]

IEqualityComparer is a .NET framework interface that allows the implementation of customized equality comparison for collections. That is, you can create your own definition of equality, and specify that this definition be used with a collection type that accepts the IEqualityComparer interface. Supported in .NET versions 3.5, 3.0, 2.0 (Source: MSDN)

This interface allows the implementation of customized equality comparison for collections. That is, you can create your own definition of equality, and specify that this definition be used with a collection type that accepts the IEqualityComparer interface. In the .NET Framework, constructors of the Hashtable, NameValueCollection, and OrderedDictionary collection types accept this interface.

This interface supports only equality comparisons. Customization of comparisons for sorting and ordering is provided by the IComparer interface.

For the generic version of this interface, see System.Collections.Generic.IEqualityComparer(Of T).

The following code example demonstrates the implementation of a case-insensitive IEqualityComparer. In this example, the CaseInsensitiveComparer.Compare method is used to determine whether two objects are equal, based on the provided CultureInfo.

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    publicnewbool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            returntrue;
        }
        else
        {
            returnfalse;
        }
    }

    publicint GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

Source: MSDN

282 questions
0
votes
1 answer

Group linq results by value and group null or invalid values by empty string

I am trying to group by a partial zip code and if any are zip codes that are null or less than 3 characters group them as "" I've seen some example of using a nullable comparer but not sure of how to fit something like that in the syntax in the…
Hank
  • 2,456
  • 3
  • 35
  • 83
0
votes
1 answer

issue with EqualityComparer for KeyValuePair

I have a class with KeyValuePair, following implementation of IEquatable does not work as I expected. and my unit test fails. I want to know why test fails? class: public class MyClass : IEquatable { public KeyValuePair
Mojtaba Khooryani
  • 1,763
  • 1
  • 17
  • 36
0
votes
2 answers

Using a IEqualityComparer for Anoymous Type in a LINQ GroupBy function

I have a IEnumerable of anonymous type as result of a LINQ join operation. Some of the values of the list are: { CellId = 0, CellIndex = "1", CellDataType = "String", CellValue = "Id", RowNumber = 0 } { CellId = 1, CellIndex = "2",…
0
votes
2 answers

Compare two objects with custom type in C#

I want to compare two objects with custom type and return the data that has a difference. I override Equals and GetHashCode in Address class and implement a ValueComparer, however the code below returns all data. Please see expected result…
cris gomez
  • 131
  • 1
  • 15
0
votes
0 answers

EqualityComparer with stable HashCode and option to ignore diacritics?

I'm looking for a IEqualityComparer that supports stable HashCode, ie the same HashCode between executions/processes. I also need it to ignore casing and nonspacing combining characters (such as diacritics). Are there any "easy" ways of…
Andreas Zita
  • 7,232
  • 6
  • 54
  • 115
0
votes
1 answer

Convert EqualityComparer> into EqualityComparer

I have such hierarchy : public interface INode { //... } public interface INode : INode { //... } public class Node : INode { //... } public class Node : Node, INode { //... } Now I want to cast like this…
Parsa
  • 7,995
  • 2
  • 27
  • 37
0
votes
1 answer

HashSet item can be changed into same item in Set

I have a Node class : public class Node : INode { public object Value { get; set; } } And I have EqualityComparer for this Node class like this : public class INodeEqualityComparer : EqualityComparer { private…
Parsa
  • 7,995
  • 2
  • 27
  • 37
0
votes
1 answer

UnorderedEquals method to compare 2 lists fails on Boolean values

I use an UnorderedEquals extension to compare 2 lists. It works well, except where the only difference is a Boolean value. Here is the compare method: public static bool UnorderedEquals( this IEnumerable list1, IEnumerable list2,…
Scho
  • 351
  • 1
  • 2
  • 12
0
votes
2 answers

IEqualityComparer error

I'm following a tutorial from C# 7.0 in a Nutshell and getting an error that I can't fix. The way that I understood the error is that the method needs to be an abstract method, which I also tried, but that didn't resolve the error. And I thought…
rds80
  • 631
  • 1
  • 10
  • 23
0
votes
2 answers

LINQ without IEqualityComparer implementation

I have 2 collection with different classes. MyClass1 - Name,Age,etc MyClass2 - Nick, Age, etc I want to find except of this collections. Something like list1.Exept(list2, (l1,l2) => l1.Name==l2.Nick); But i cannt write this code and need to…
Neir0
  • 12,849
  • 28
  • 83
  • 139
0
votes
1 answer

LINQ Distinct Not Working

I'm trying to select distinct instances from my class but it's not working, even after implements IEqualityComparer (as described here: https://msdn.microsoft.com/en-us/library/bb338049.aspx) AND IEquatable Could someone help? Public Class Teste …
0
votes
1 answer

Unit Testing in Visual Studio: what to implement in my class to be able to use Assert.AreEqual?

I'm adding unit testing to my (Visual Basic) project. I'm using the testing tools in Visual Studio (2010 Premium). In a couple of test I would like to make sure that my class is equal to the expected value of the class with Assert.AreEqual. But this…
0
votes
1 answer

Can't get Distinct and IEqualityComparer to work in ASP.net 6.1

I'm trying to remove duplicate entries when email address and phone number match. I've tried several solutions and I can't get anything to work. I've even tried the C# sample from the link posted…
Dumber_Texan2
  • 840
  • 2
  • 12
  • 34
0
votes
1 answer

Grouped custom object with a list property

I have a list of customObject, I want to group the "CustomObject" by the List property of the CustomObject object. public class CustomObject { public string Name { get; set; } public List List { get; set; } public…
Axel
  • 21
  • 7
0
votes
2 answers

Creating a custom equality comparer for IEnumerables when T is IEnumerable

I want to have a custom equality comparer IEnumerables. using @PaulZahra's code, I created the following class: class CustomEqualityComparer : IEqualityComparer> { public bool Equals(IEnumerable x, IEnumerable y) { …
Michael Haddad
  • 4,085
  • 7
  • 42
  • 82