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 );