Removing duplicates from list<t> in C #/linq distinct duplicates

Source: Internet
Author: User

List<t> is one of the most commonly used data structures in. NET, and we often put objects that need to be manipulated into a list<t>. Sometimes we need to keep the data in the list<t> unique, which means that the data in the list cannot have duplicates. We know that,list<t> can be stored in any type, such as list<int>,list<string>. In order to eliminate duplicates in the list<t>,. NET provides us with a distinct () function. For a normal type of list, we can call the function directly to eliminate duplicates in the list. Here is the sample code:

list<int> intinlist =Newlist<int> () {1,7,2,4,2,0,2,7 }; List<int> newintlist =intinlist.distinct (). ToList ();foreach(intIinchnewintlist) {Console.Write (i+"\ t");}

Here are the data results:

1       7       2       4       0       Continue ...

It shows the most basic list of list<int>, but if we have a custom class like the person class at this point, the above code will not get the correct result. The code is as follows:

 Public classperson{ Public stringFirstName;  Public stringLastName;} classprogram{Static voidMain (string[] args) {List<Person> personinlist =NewList<person>(); Personinlist.addrange (Newperson[]{NewPerson () {FirstName="Sheldon", LastName="Liu"            },            NewPerson () {FirstName="Aaron", LastName="Zeng"            },            NewPerson () {FirstName="Sheldon", LastName="Liu"            },            NewPerson () {FirstName="Aaron", LastName="Zeng"            },        }); Personinlist=personinlist.distinct ().        ToList (); foreach(varPinchpersoninlist)        {Console.WriteLine (p.firstname); }     }}

Here is the result of the operation:

Continue ...

As you can see, we have not removed the duplicate elements. The reason is. NET has no way to compare the equality of two person instances. In order to let. NET knows who the two person is, we need to write another helper class that implements the Iequalitycomparer<person> interface and then passes the instance of that class to distinct (). So,. NET has the ability to determine if the two person instances are equal, and can eliminate duplicates from the list<person>. The code is as follows:

 Public classperson{ Public stringFirstName;  Public stringLastName;} Public classPersoncomparer:iequalitycomparer<person>{     Public BOOLEquals (person x, person y) {if(X.firstname.equals (Y.firstname) &&x.lastname.equals (y.lastname))return true; Else            return false; }     Public intGetHashCode (Person obj) {returnObj. Firstname.gethashcode () *obj.    Lastname.gethashcode (); }}classprogram{Static voidMain (string[] args) {List<Person> personinlist =NewList<person>(); Personinlist.addrange (Newperson[]{NewPerson () {FirstName="Sheldon", LastName="Liu"            },            NewPerson () {FirstName="Aaron", LastName="Zeng"            },            NewPerson () {FirstName="Sheldon", LastName="Liu"            },            NewPerson () {FirstName="Aaron", LastName="Zeng"            },        }); Personinlist= Personinlist.distinct (Newpersoncomparer ()).        ToList (); foreach(varPinchpersoninlist)        {Console.WriteLine (p.firstname); }     }}

The results of the operation are as follows:

Continue ...

Removing duplicates from list<t> in C #/linq distinct duplicates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.