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
2
votes
1 answer

ICollection<>.Contains in EF as HashSet fails

I have an object defined as such: public class QuestionSetAssignee { [Required] public int Id { get; set; } [Required] public string Name { get; set; } public virtual ICollection QuestionSets { get; set; } …
David Poxon
  • 2,435
  • 4
  • 23
  • 44
2
votes
3 answers

Compare and merge two List into new List

I'm trying to figure out how best to compare and merge two List with a new List being generated that compares multiple properties within each object. class Account { public Account() { } public string ID { get; set; } public string…
2
votes
1 answer

LINQ to Entities does not recognize the method 'Boolean Contains

I have written this statement: if (!db.Customers.Contains(customer,customerCompairor)) { db.Customers.Add(customer); } Don't see a reason why getting following error: LINQ to Entities…
Simsons
  • 12,295
  • 42
  • 153
  • 269
2
votes
2 answers

How do you create a dynamic equality implementation where you can pass in the property names to be compared?

Say I have an object Person with the properties below: public class Person { public int ID { get; set; } public int EmployeeNo { get; set; } public string JobDescription { get; set; } public string Code { get;…
SamuelDavis
  • 3,312
  • 3
  • 17
  • 19
2
votes
1 answer

Not-hash-based set collection for storing unique objects with custom equality comparer - C#

I'm trying to store (name: string, value: long) pair in a set. public class NameValuePair { public string name; public long value; } public NameValuePairComparer comparer = new NameValuePairComparer(); public HashSet nameValueSet…
Mariusz Ceier
  • 33
  • 1
  • 4
2
votes
1 answer

Subclass HashSet so that it always uses a certain IEqualityComparer when used in another set

I want to subclass HashSet so that it uses HashSet.CreateSetComparer() as an IEqualityComparer whenever I use it inside another set. Basically every time I do this: var myDict = new Dictionary(); I want it …
Flash
  • 15,945
  • 13
  • 70
  • 98
2
votes
1 answer

IEquatable(Of T)/IEqualityComparer(Of T) Not Being Called

I have some code with two lists of objects. The first list is more inclusive than the second list. I wish to exclude items in the second list from the first list. After some research I found that the extension method Except is the way to do this. I…
cjbarth
  • 4,189
  • 6
  • 43
  • 62
2
votes
4 answers

IEnumerable.Except() and a custom comparer

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…
kanko
2
votes
2 answers

LINQ GroupBy on multiple ref-type fields; Custom EqualityComparer

So I've looked through about 20 examples on this on SO and elsewhere, but haven't found one which covers what I'm trying to do. This - Can I specify my explicit type comparator inline? - looks like what I need, but doesn't go far enough (or I don't…
Peter
  • 5,455
  • 7
  • 46
  • 68
2
votes
4 answers

LINQ Except using custom Comparer

I am trying to use the "Except" method on a LINQ result set using a custom implementation if IEqualityComparer to exclude certain results based on the value of a single field from the result set. So, in simplified form I have... '' Get collection of…
FourOaks
  • 190
  • 1
  • 3
  • 7
1
vote
3 answers

What is the AccessTime for Dictionary,List> , is it still O(1)?

I wanted to implement an algorithm with Dictionary, List> to find the anagram words in a dictionary. As i need to implement my custom EqualityComparer for this Dictionary, is the access time still O(1) i.e big O (1)…
ioWint
  • 1,609
  • 3
  • 16
  • 34
1
vote
2 answers

Comparing two custom Objects (using IEqualityComparer)

Firstly, I will say what I want to compare the following: My Custom Object (Item) has a List of strings taxids. I want to look if all the strings in one List occur in another list of strings (will be also another taxids of another Object (Item).…
Ozkan
  • 2,011
  • 6
  • 29
  • 43
1
vote
2 answers

Create an iqueryable list that's distinct

I have a list of objects, GroupStudentStatus, that I need to make distinct. I wrote the class below to do this. The 2 attributes that are relevant are GroupStudentStatus.IsLastActionRemoved (DateTime) and GroupStudentStatus.Student.Guid. protected…
Brad8118
  • 4,672
  • 11
  • 36
  • 48
1
vote
2 answers

Why is there no Overload of Distinct() accepting a Comparison Delegate (or similar)

When using the query operator Distinct() the types in the queried sequence must either provide suitable overloads of GetHashCode() and Equals() or you have to pass an implementation of IEqualityComparer. My question: Why is there no overload of…
Nico
  • 1,554
  • 1
  • 23
  • 35
1
vote
1 answer

Does .NET Hashtable uses the specialized Font equality comparer when fonts are used as keys?

I need to create a set of unique Font objects each of which should have an associated object. This should be done in a .NET Framework WinForms app. These Font objects come from various sources, and different Font objects can be actually the same -…
TecMan
  • 2,743
  • 2
  • 30
  • 64