First, we will give an introduction to the IEqualityComparer interface in MSDN. Click the open link. IEqualityComparer is mainly applicable to defining methods to support equal object comparison. You can achieve custom equality comparison of sets. That is, you can create your own equality definition and specify this definition to be used together with the collection type that accepts the IEqualityComparer interface.
The IEqualityComparer Interface contains two methods:
Description
Equals determines whether the specified object is equal.
GetHashCode returns the hash code of the specified object.
Overall, it is easier to understand
Equals: Self-inverse, symmetric, and passed. That is to say, if this method is used to compare an object with itself, it returns true; if this method is executed on y and x, true is returned, returns true for both objects x and y. returns true if you execute this method on x and y, and returns true if you execute this method on y and z, returns true for the x and z objects.
To implement this function, make sure that if the Equals method is executed on both objects x and y returns true, the values returned by the GetHashCode method executed on x and y must be equal.
<// Www.w3.org/5o/xhtml:sentencetext xmlns = "http://www.w3.org/1999/xhtml">
GetHashCode method: to implement this method, make sure that if the Equals method returns true for the two objects x and y, the values returned by the GetHashCode Method on x and y must be equal.
<// Www.w3.org/5o/xhtml:sentencetext xmlns = "http://www.w3.org/1999/xhtml"> when we operate our custom object using Linq, we will find that some methods do not work directly, for example: extended methods such as Distinct, Except, and Intersect. Therefore, you need to define the IEqualityComparer interface to determine the equality between the two objects.
The following is a simple example:
First, we define a class. The class is as follows:
[Csharp]
Public class SellerTypeIdentify
{
/// <Summary>
/// Merchant type ID
/// </Summary>
Public string SellerTypeID
{
Get;
Set;
}
Public string SellerName
{
Get;
Set;
}
}
Define a comparator based on SellerTypeID
[Csharp]
Public class SellerTypeIdentifyComparer: IEqualityComparer <SellerTypeIdentify>
{
// SellerType are equal if their names and product numbers are equal.
Public bool Equals (SellerTypeIdentify x, SellerTypeIdentify y)
{
// Check whether the compared objects reference the same data.
If (Object. ReferenceEquals (x, y) return true;
If (Object. ReferenceEquals (x, null) | Object. ReferenceEquals (y, null ))
Return false;
Return x. SellerTypeID = y. SellerTypeID;
}
Public int GetHashCode (SellerTypeIdentify product)
{
// Check whether the object is null
If (Object. ReferenceEquals (product, null) return 0;
// Get hash code for the SellerTypeID field if it is not null.
Int SellerTypeId = product. SellerTypeID = null? 0: product. SellerTypeID. GetHashCode ();
// Calculate the hash code for the SellerType.
Return SellerTypeId;
}
}
Initialize data
[Csharp]
Public static List <SellerTypeIdentify> GetMainData ()
{
List <SellerTypeIdentify> sellerTypeList = new List <SellerTypeIdentify> ();
SellerTypeList. Add (new SellerTypeIdentify {SellerTypeID = "00000001", SellerName = "Name0001 "});
SellerTypeList. Add (new SellerTypeIdentify {SellerTypeID = "00000002", SellerName = "Name0002 "});
SellerTypeList. Add (new SellerTypeIdentify {SellerTypeID = "00000003", SellerName = "Name0003 "});
Return sellerTypeList;
}
Public static List <SellerTypeIdentify> GetSuppData ()
{
List <SellerTypeIdentify> sellerTypeList = new List <SellerTypeIdentify> ();
SellerTypeList. Add (new SellerTypeIdentify {SellerTypeID = "00000002", SellerName = "Name0001 "});
SellerTypeList. Add (new SellerTypeIdentify {SellerTypeID = "00000001", SellerName = "Name0002 "});
SellerTypeList. Add (new SellerTypeIdentify {SellerTypeID = "00000003", SellerName = "Name0003 "});
SellerTypeList. Add (new SellerTypeIdentify {SellerTypeID = "00000004", SellerName = "Name0004 "});
Return sellerTypeList;
}
Take Distinct and SequenceEqual for example.
[Html]
List <SellerTypeIdentify> sellerTypeMainList = new List <SellerTypeIdentify> ();
SellerTypeMainList = Program. GetMainData ();
List <SellerTypeIdentify> sellerTypeSuppList = new List <SellerTypeIdentify> ();
SellerTypeSuppList = Program. GetSuppData ();
Int mainSellerCount = sellerTypeMainList. Distinct (new SellerTypeIdentifyComparer (). Count ();
Int suppSellerCount = sellerTypeSuppList. Distinct (new SellerTypeIdentifyComparer (). Count ();
List <bool> xquery = new List <bool> ();
If (suppSellerCount <= mainSellerCount)
{
Foreach (SellerTypeIdentify s in sellerTypeSuppList. Distinct (new SellerTypeIdentifyComparer (). OrderBy (x => x. SellerTypeID ))
{
Xquery. Add (sellerTypeMainList. OrderBy (x => x. SellerTypeID). Contains (s, new SellerTypeIdentifyComparer ()));
}
}
Else
{
Console. WriteLine ("NOT CONTAINS1 ");
}
If (xquery. Contains (false ))
{
Console. WriteLine ("NOT CONTAINS2 ");
}
Bool IsEqual = sellerTypeMainList. OrderBy (x => x. SellerTypeID). SequenceEqual (sellerTypeSuppList. OrderBy (x => x. SellerTypeID), new partition ());
If (IsEqual)
{
Console. WriteLine ("Equal ");
}
Else
{
Console. WriteLine ("Not Equal ");
}
The output result is as follows: