The Enumerable.distinct method is a common LINQ extension method that belongs to the System.Linq Enumerable method, which can be used to remove duplicate elements from an array, a collection, and to customize the deduplication rules.
There are two overloaded methods:
Summary://The value is compared by using the default equality comparer to return non-repeating elements in the sequence. Parameter://Source://The sequence from which the repeating element is to be removed. Type parameter://TSource://type of element in source. Returns the result://A SYSTEM.COLLECTIONS.GENERIC.IENUMERABLE<T>, containing a non-repeating element in the source sequence. Exception://System.ArgumentNullException://source is null. public static ienumerable<tsource> distinct<tsource> (this ienumerable<tsource> source); Summary://The value is compared by using the specified system.collections.generic.iequalitycomparer<t> to return non-repeating elements in the sequence. Parameter://Source://The sequence from which the repeating element is to be removed. Comparer://system.collections.generic.iequalitycomparer<t> for comparing values. Type parameter://TSource://type of element in source. Return Result://a SYSTEM.COLLECTIONS.GENERIC.IENUMERABLE< T>, containing non-repeating elements in the source sequence. Exception://System.ArgumentNullException://source is null. public static ienumerable<tsource> distinct<tsource> (this ienumerable<tsource> source, Iequalitycomparer<tsource> comparer);
The first method takes no arguments, and the second method needs to pass a System.collections.generic.iequalitycomparer<t> implementation object
1. Value type element collection de-weight
list<int> list = new List<int> {1, 1, 2, 2, 3, 4, 5, 5};list. Distinct (). ToList (). ForEach (s = Console.WriteLine (s));
Execution results are: 1 2 3 4 5
2. Reference type element collection go to weight
First, customize a student class
public class Student {public string Name {get; private set;} public int Id {get; private set;} public string Hobby {get; private set;} Public Student (string name, int ID, string hobby) {this . name = name; This. id = ID; This. Hobby = Hobby; } <summary>/// convenient output, override ToString Method///</summary>// <returns></returns> Public override String ToString () { return string. Format ("{0}\t{1}\t{2}", this. Name, this. Id, this. Hobby); } }
Use the distinct method without parameters to remove the weight
list<student> list = new list<student> () { new Student ("James", 1, "Basketball"), new Student (" James ", 1," Basketball "), new Student (" Kobe ", 2," Basketball "), new Student (" Curry ", 3," Football "), New Student ("Curry", 3, "Yoga") }; List. Distinct (). ToList (). ForEach (s = Console.WriteLine (S.tostring ()));
Execution Result:
Visible, and no duplicate records are removed.
The distinct method without the comparer parameter is compared by using the default comparer of the IEqualityComparer interface, for reference types, the default comparer compares its reference address, and each element in the assembly is a new instance, and the reference address is different. So it will not be deleted as a duplicate record.
Therefore, we consider the use of the second overloaded method.
Create a new class that implements the IEqualityComparer interface. Note the implementation of the GetHashCode method, only hashcode the same will be compared
public class compare:iequalitycomparer<student> {public bool Equals (Student x,student y) { return x.id = = y.id;//can customize the deduplication rule, where the same Id as the duplicate record, regardless of the student's hobby is what } public int GetHashCode (Student obj) { return obj. Id.gethashcode (); } }
and then call
List. Distinct (New Compare ()). ToList (). ForEach (s = Console.WriteLine (S.tostring ()));
Execution Result:
We follow the ID to give this set to go to success!
3. How to write a scalable weight-down method
public class Compare<t, c>: iequalitycomparer<t> {private func<t, c> _getfield; Public Compare (func<t, c> getfield) {This._getfield = GetField; } public bool Equals (T X, t y) {return equalitycomparer<c>. Default.equals (_getfield (x), _getfield (y)); } public int GetHashCode (T obj) {return equalitycomparer<c>. Default.gethashcode (This._getfield (obj)); }} public static class Commonhelper {//<summary>///Custom distinct extension method///</summ ary>//<typeparam name= "T" > To go to the heavy object class </typeparam>///<typeparam Name= "C" > Custom de-weight field type < /typeparam>//<param name= "source" > Objects to go to heavy </param>//<param name= "GetField" > Get custom de-heavy words Delegate of paragraph </param>///<returns></returns> public static ienumerable<t> mydistinct<t, C > (This IENumerable<t> source, func<t, c> GetField) {return source. Distinct (New compare<t, c> (GetField)); } }
Call:
List. Mydistinct (s=>s.id). ToList (). ForEach (s = Console.WriteLine (S.tostring ()));
The use of generics, delegates, extension methods and other knowledge points. Various de-weight scenarios that can be used with any collection
Reprint Source: https://www.cnblogs.com/Robert-go-go/p/5399198.html
"C #" list to go Heavy (reprint)