VS2005 has a well-packaged deduplication approach to the DataTable:
1 //Remove Duplicate Rows2DataView DV =table. DefaultView;3Table = dv. ToTable (true,New string[] {"name","Code" });4 5 at this point, the table has only name, code no duplicate two lines, if you also need the ID value6 7Table = dv. ToTable (true,New string[] {"Id","name","Code"});//First parameter true enable de-duplication, similar to distinct
If there is a set of data (ID is not a unique field)
ID name code1 Zhang San 1232 John Doe 456 3 Zhang San 4561 Zhang San 123
Through the above method to get
ID name code1 Zhang San 1232 John Doe 4563 Zhang San 456
To repeat the removal of only the ID name code completely duplicate rows, if you want to filter the data is only the name is not allowed to repeat it?
Table = dv. ToTable (truenewstring"name"});
Get:
Name Zhang San John Doe
But the result I want is to repeat for only one of the column name columns, and to show the other columns
The desired result is:
ID name code1 Zhang San 1232 John Doe 456
How does this come true? The following method can be, perhaps there is a better way, I hope you have a lot of advice
1 #regionDelete a DataTable repeating column, similar to distinct2 /// <summary> 3 ///Delete a DataTable repeating column, similar to distinct4 /// </summary> 5 /// <param name= "DT" >DataTable</param> 6 /// <param name= "Field" >Field name</param> 7 /// <returns></returns> 8 Public StaticDataTable Deletesamerow (DataTable DT,stringField)9 {TenArrayList indexlist =NewArrayList (); One //find the row index to delete A for(inti =0; i < dt. Rows.Count-1; i++) - { - if(!Iscontain (indexlist, i)) the { - for(intj = i +1; J < dt. Rows.Count; J + +) - { - if(dt. Rows[i][field]. ToString () = =dt. Rows[j][field]. ToString ()) + { - Indexlist.add (j); + } A } at } - } - Indexlist.sort ();
// sort - for(inti = Indexlist.count-1; I >=0; i--)// Delete rows According to the list of indexes to be deleted - { - intindex =Convert.ToInt32 (Indexlist[i]); in dt. Rows.removeat (index); - } to returnDT; + } - the /// <summary> * ///determine if the array exists $ /// </summary> Panax Notoginseng /// <param name= "Indexlist" >Array</param> - /// <param name= "index" >Index</param> the /// <returns></returns> + Public Static BOOLIscontain (ArrayList indexlist,intindex) A { the for(inti =0; i < Indexlist.count; i++) + { - intTempindex =Convert.ToInt32 (Indexlist[i]); $ if(Tempindex = =index) $ { - return true; - } the } - return false;Wuyi } the #endregion
Delete a DataTable repeating column for deleting only one of the columns in duplicate rows