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

How to element-wise compare arrays in LINQ functions

My issue is the following: Dim dt As DataTable = GetSomeDataTable() Dim exceptions As List(Of String())({{"a","b"},{"c","d"}}) For Each j As Integer In Enumerable.Range(0, dt.Rows.Count) _ .Where(Function(j1)…
user2163043
  • 361
  • 1
  • 3
  • 14
0
votes
1 answer

using long (int64) as a hashCode and still use IEqualityComparer for concurrent Dictionary

I have a problem using a self made IEqualityComparer and GetHashCode in a concurrent dictionary. The class below (simplified with used two properties) works perfect when I implement it like this: ConcurrentDictionary
user369122
  • 792
  • 3
  • 13
  • 33
0
votes
1 answer

Linq to SQL: Partial matching strings using contains on an array of strings

Here is my code: string[] customerNames = searchModel.CustomerName.Split(','); query = query.Where(d => customerNames.Contains(d.CustomerName, comparer) || customerNames.Contains(d.Company1.CompanyName, comparer)); Which works if you are just…
Dave
  • 1,645
  • 2
  • 23
  • 39
0
votes
2 answers

Definition of GetHashCode() in C#

Dictionary in C# uses GetHashCode() to retrieve hash code of a given key. I walk through whole of the Dictionary Class, but there is not any definition for GetHashCode() function. It is bothering me and I don't know how I can find definition of it…
Chavoosh
  • 425
  • 4
  • 15
0
votes
2 answers

Why is EqualityComparer.Default not working?

var dic = context.Treasure.Include("TreasureShare") .Where(t => t.TreasureShare.IsShared && t.TreasureShare.EvaluationContent.Contains(keyword)) .ToDictionary(t => t.ProductUrl, t => t.ProductId, EqualityComparer.Default); I got an…
shimron
  • 596
  • 6
  • 19
0
votes
0 answers

Edit SQL Command from IQueryable

How I can edit SQL Command from IQueryable? I need edit ON of JOIN, because Linq to Entities dont allow use IEqualityComparer. I need to edit the command string and then put back in IQueryable.
user2965241
  • 53
  • 1
  • 5
0
votes
2 answers

Linq to Entities Non-Equal Join

I must perform the following SQL command using IQueryable: SELECT * FROM myTable t1 INNER JOIN myTable t2 ON t1.time = t2.time + 1 OR t1.time = t2.time + 2 OR t1.time = t2.time + 3; Linq to Entities does not allow me to use…
user2965241
  • 53
  • 1
  • 5
0
votes
1 answer

Custom Equals function not being called when using IEqualityComparer

I have created a IEqualityComparer for my classes that represent objects from a database and I wanted to override the Equals method of these classes so it'd compare the Id of the object when calling Equals, Contains etc. The problem is that my…
Darthtong
  • 1,017
  • 4
  • 18
  • 30
0
votes
2 answers

Comparing Each Character in Java String

I am a beginner at C++, and I am trying to create two strings any suggestion?
0
votes
2 answers

Strange behaviour on IEqualityComparer

I'm trying to implement an IEqualityComparer for my object that basically detects if an object is older that another one. The following simpler example will synthesises what i'm trying to accomplish: class Program { static void Main(string[]…
0
votes
0 answers

add Equality Comparer class to base class for custom property classes in c#

i'm using the ConcurrentDictionary were the key is made of a class with public properties. after playing around with the code from (HashCode on decimal with IEqualityComparer in a ConcurrentDictionary) I wanted to find a solution, so I don't have to…
0
votes
1 answer

How to implement a comparer on a dictionary value

I have: Dictionary ItemList = new Dictionary(); Where MyClass is something like: public class MyClass { public int BaseItemID; public string Description; public MyClass() { } public class…
webnoob
  • 15,747
  • 13
  • 83
  • 165
0
votes
1 answer

IEqualityComparer in Union method with annonymouse type

I wrote a query like this: context.PageGroupLangsInSettings .Where(x => x.PageGroupLang.Language.CaltureId == langCaltureId && x.PageGroupLang.PageGroup.Id == pageGroupId) .Select(x => new { x.Key, x.Value, Order = 0 }) …
Saeed Hamed
  • 732
  • 2
  • 10
  • 28
0
votes
0 answers

HashCode on decimal with IEqualityComparer in a ConcurrentDictionary

I made a class to be used as a key in a dictionary. public class FourUintsOneDecimalKeyInfo { public uint IdOne { get; set; } public uint IdTwo { get; set; } public uint IdThree { get; set; } public uint IdFour { get; set; } …
user369122
  • 792
  • 3
  • 13
  • 33
0
votes
2 answers

c# getting a configurable equatable method

I've got a simple factory that's built in C# that instantiates and configures validators that are built in ASP.net and JavaScript. I want a way to test if I'm accidently trying to set a validator twice (for example, having two…
Izzy
  • 1,764
  • 1
  • 17
  • 31