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
1
vote
2 answers

What is the difference between an IQueryable's Contains() and an IEnumerable's Contains()?

I have an IQueryable custs, a Customer cust, a CustomerComparer custCp which implements IEqualityComparer. When I call custs.Contains(cust, custCp) I get an exception: System.NotSupportedException: Unsupported overload used for query operator…
Jimbo
  • 22,379
  • 42
  • 117
  • 159
1
vote
2 answers

How to do dynamic comparison using IEqualityComparer?

I have a person class like so: Public Class Person { public int Id {get; set;} public int FirstName {get; set;} public int LastName {get; set;} } I create a list of Person objects: List AllPersons = GetAllPersons(); In GetAllPersons() I…
Codehelp
  • 4,157
  • 9
  • 59
  • 96
1
vote
3 answers

Correct way to override Equals(object obj) when dealing with sub/superclasses?

I am using the following code to test for Equals public override bool Equals(object obj) { // Equals must return false on compares to null. if (obj == null || GetType() != obj.GetType()) return false; Foo fooItem = obj as…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
1
vote
3 answers

get distinct item in List with highest value in property

I have an object AppDetail containing 2 string properties, a name, and a version string (e.g. "1.0.0") Given a List< AppDetail > that contains duplicates of the same name but different version strings, how do I create a List with unique names, and…
Amc_rtty
  • 3,662
  • 11
  • 48
  • 73
1
vote
2 answers

How do I get a Distinct list to work with EF 4.x DBSet Context and the IEqualityComparer?

I have been trying for hours to get a Distinct to work for my code. I am using EF 4.3, MVC3, Razor and trying to get a list downto product id and name. When I run the Sql query against the DB, it's fine. Sql Query is SELECT DISTINCT [ProductId] …
DavieDave
  • 1,394
  • 2
  • 18
  • 35
0
votes
2 answers

IEqualityComparer and custom type

I'm trying to compare custom type in two List and use the Intersect / Except method. The equality is determined by three fields of this type. The equality is based on more than the ordinary condition (all fields contains the same data). I…
Bes-m M-bes
  • 177
  • 1
  • 4
  • 16
0
votes
2 answers

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

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…
David
  • 15,150
  • 15
  • 61
  • 83
0
votes
2 answers

Correct usage of GetHashCode in IEqualityComparer

Let's say I have this example: public class Player { public string Username { get; set; } private sealed class PlayerEqualityComparer : IEqualityComparer { public bool Equals(Player x, Player y) { …
Alessandro
  • 97
  • 6
0
votes
1 answer

PANDAS/Python check if the value from 2 datasets is equal and change the 1&0 to True or False

I want to check if the value in both datasets is equal. But the datasets are not in the same order so need to loop through the datasets. Dataset 1 contract : enter image description here Part…
0
votes
1 answer

Why IEqualityComparer doesn't work as expected?

Why EqualityComparer MyEqComp doesn't work? It should left in arr only 2 elements with equal Values - myDic["1"]and myDic["3"] with Value="456". But instead it consists of myDic["1"] and myDic["2"] but their Values are not equal. Function…
NoImagination
  • 178
  • 1
  • 8
0
votes
1 answer

C# Dictionary(IEqualityComparer?comparer) constructor example

I have tried to study the below mentioned constructor example of C# in Microsoft Documentation, however I am not able to understand it: Dictionary(IEqualityComparer?comparer) Can anyone explain me what can be a good example…
0
votes
2 answers

C# HashSet not using defined IEqualityComparer<>

I am trying to compare two HashSets of objects by using .SetEquals(). I looked at some guides and saw that I needed to implement IEqualityComparer<> as part of the class in order for it to work properly. However, I have found that .SetEquals() is…
Unknown
  • 141
  • 7
0
votes
1 answer

Implementing IEqualityComparer

I've added to my class the IEqualityComparer implementation, not sure if the code bellow is the correct one, especially the Equals(object x, object y) functions: should we override or make a new implementation of the Equals method, like this:…
serge
  • 13,940
  • 35
  • 121
  • 205
0
votes
3 answers

What is the clean way for checking null equality for a class implementing IEqualityComparer?

This is what I could come up with to check for null equality. It works but it is Shreak looking. The following is valid for this example: x != null and y == null returns false; x == null and y != null returns false; x == null and y == null returns…
RollRoll
  • 8,133
  • 20
  • 76
  • 135
0
votes
3 answers

Given a custom generic class that stores a List how do I prevent adding an object of type T more than once to the List?

So this is the code that I have tried, but it adds the same object more than once: namespace TestComparison { public interface IAddable { int RandomIntValue { get; set; } // often Times this value will repeat. } public class…
Hidemat
  • 19
  • 5