I have the following courses
public class ModInfo : IEquatable<ModInfo> { public int ID { get; set; } public string MD5 { get; set; } public bool Equals(ModInfo other) { return other.(MD5); } public override int GetHashCode() { return (); } }
I load some data into the list of the class using the following method:
public void ReloadEverything() { var beforeSort = new List<ModInfo>(); // Bunch of loading from local sqlite database. // not included since it's reload boring to look at var modinfo = (m => ).AsEnumerable().Distinct().ToList(); }
The problem is that the Distinct() call does not seem to do this. There are still objects that are equal to each other.
According to this article:/en-us/library/vstudio/bb348436%28v=vs.100%
This is how you should do different work, but it doesn't seem to call the Equals method on the ModInfo object.
What causes this to happen?
Example values:
modinfo[0]: id=2069, MD5 =0AAEBF5D2937BDF78CB65807C0DC047C
modinfo[1]: id=2208, MD5 = 0AAEBF5D2937BDF78CB65807C0DC047C
I don't care which value to choose, because the md5 values are the same, they may be the same.
You also need to override, not just implement IEquatable.
If you add it to your class:
public override bool Equals(object other) { ModInfo mod = other as ModInfo; if (mod != null) return Equals(mod); return false; }
It should work.
For more information, see this article:Implementing IEquatable Properly
Edit: OK, this is a slightly different implementation based on GetHashCode best practices.
public class ModInfo : IEquatable<ModInfo> { public int ID { get; set; } public string MD5 { get; set; } public bool Equals(ModInfo other) { if (other == null) return false; return (this.(other.MD5)); } public override int GetHashCode() { unchecked { int hash = 13; hash = (hash * 7) + (); return hash; } } public override bool Equals(object obj) { ModInfo other = obj as ModInfo; if (other != null) { return Equals(other); } else { return false; } } }
You can verify it:
ModInfo mod1 = new ModInfo {ID = 1, MD5 = "0AAEBF5D2937BDF78CB65807C0DC047C"}; ModInfo mod2 = new ModInfo {ID = 2, MD5 = "0AAEBF5D2937BDF78CB65807C0DC047C"}; // You should get true here bool areEqual = (mod2); List<ModInfo> mods = new List<ModInfo> {mod1, mod2}; // You should get 1 result here mods = ().ToList();
What's with those specific numbers in GetHashCode?
Supplementary knowledge:Deduplicate List collections through Distinct method in C#
In the List collection object in C#, the Distinct method can be used to deduplicate the List collection elements. If the elements inside the list collection are of value type, the Distinct method judges the deduplication based on whether the value types are equal. If the elements inside the List collection are reference type variables, it is determined that the objects with the same reference are the same and performs the List collection element deduplication operation.
(1) List collection object intList of value type, internal elements are 1, 1, 2, 2, 3, 4, 5 and other elements. To deduplicate an intList object, use the following statement:
List intList= new List() { 1, 1,2,2,3,4,5};
intList= ().ToList();
After the above Distinct method deduplication and reassignment, the internal elements of the intList collection are: 1, 2, 3, 4, 5.
(2) Deduplication of the Distinct method for reference type is to determine whether the reference address of the object in the List collection is consistent. If it is inconsistent, it is two different objects, even if the attribute values of each of the two objects are the same.
List testList = new List<>(); (new TestModel() { Index=1, Name=“Index1” }); (new TestModel() { Index = 2, Name = “Index2” }); (new TestModel() { Index = 2, Name = “Index2” }); testList = ().ToList();
In the above program statement, although the attribute values of the second element and the third element in the List collection testList are exactly the same, these two elements are still different objects. Therefore, after calling the Distinct() method to deduplicate, the testList remains unchanged.
The above article on c# Linq distinct does not call the Equals method is all the content I share with you. I hope you can give you a reference and I hope you support me more.