Use a proxy to perform C # list distinct operations

Source: Internet
Author: User

The model is often used in c # programming, and the list is often used to store entity sets. Therefore, various list operations are designed, and list sorting and searching are common, comparison, deduplication. In general, if you want to repeat the list, if you use the linq distinct method, you will encounter some pitfalls and find that duplicate data still exists in the result set, the reason is that this method is used to deduplicate object references and does not meet our needs. Therefore, this article uses the c # proxy method to perform the list distinct operation. First, we will introduce the traditional method of repeat the list. The Code is as follows: List <ReviewersReport> reportList = GetReportList (); for (int I = 0; I <reportList. count; I ++) {for (int j = I + 1; j <reportList. count; j ++) {if (reportList [I]. equals (reportList [j]) {reportList. removeAt (reportList. lastIndexOf (reportList [I]); j -- ;}} using this method to perform the distinct operation on the list is obviously troublesome, if other list entity sets need to implement similar functions, we will worry about code reusability. The following describes how to implement the list distinct function in a simple and efficient way. It is also recommended in this article to create a Compare class first, as shown below: public delegate bool extends scomparer <T> (T x, T y); public class Compare <T>: IEqualityComparer <T> {private extends scomparer <T> _ extends scomparer; public Compare (extends scomparer <T> extends scomparer) {this. _ ipvscomparer = ipvscomparer;} public bool Equals (T x, T y) {if (null! = This. _ ipvscomparer) return this. _ ipvscomparer (x, y); else return false;} public int GetHashCode (T obj) {return obj. toString (). getHashCode () ;}} a delegate is passed in the constructor. The caller can define a comparison rule in this delegate, which provides great flexibility, we can note that Compare implements the IEqualityComparer interface to define comparison objects and determine whether the two objects are equal. Usage: ist <ReviewersReport> requestList = Get RequestList (); requestList = requestList. Distinct (new Compare <Requestor> (x, y) => (null! = X & null! = Y) & (x. requestorName. equals (y. requestorName )))). toList (); this method greatly extends the usage range of the comparator, increases the reusability of the code, and can be applied to the comparison of any object. After comparison, we can sort the scripts to make the code concise. View Code

Related Article

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.