Delete a DataTable repeating column for deleting only one of the columns in duplicate rows

Source: Internet
Author: User

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

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.