C # Remove duplicate items in a collection

Source: Internet
Author: User

Q: What should I do if I intercept the year substring and remove repeated fields when retrieving datetime fields in the database table?

Method: Use the lambda expression in LINQ for easy implementation. The following is a simple sample code:

 

Public void bindnf ()
{
// Remove the same year, which is implemented through the lambda expression of LINQ.
DM dm = new DM ();
String strsql = "select * from [alert record]";
Dataset DS = DM. getsql (strsql );
Arraylist Al = new arraylist ();
Foreach (datarowview DRV in DS. Tables [0]. defaultview)
{
Al. add (DRV ["Release Date"]. tostring (). substring (0, DRV ["Release Date"]. tostring (). indexof ("-")));
}
// Construct a generic set
List <string> years = new list <string> ();
For (INT I = 0; I <Al. Count; I ++)
{
Years. Add (Al [I]. tostring ());
}

// The distinct () method is used to return non-repeating elements in the sequence.
VaR result = years.Distinct();

This. ddlnf. datasource = result;
This. ddlnf. databind ();
}

 

2. Another non-LINQ implementation method can also be implemented by using a general loop comparison method:

String S1 = "A, B"; <br/> string S2 = "a, B, c, d"; <br/> string [] str1 = s1.split (', '); <br/> string [] str2 = s2.split (', '); <br/> string S3 = ""; <br/> bool flag = true; <br/> foreach (string temps in str2) <br/>{< br/> flag = true; <br/> foreach (string temps2 in str1) <br/>{< br/> If (temps2 = temps) <br/>{< br/> flag = false; <br/> break; <br/>}< br/> If (FLAG) <br/>{< br/> S3 + = temp S + ","; <br/>}< br/> If (S3! = "") <Br/>{< br/> S3 = s3.remove (s3.length-1, 1); <br/>}< br/> response. Write (S3 );

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.