Remove the DataTable duplicate column removing only one of the duplicate rows in the column

  • 2020-05-26 08:14:35
  • OfStack

vs2005 has an encapsulated method to repeat for datatable:


// Get rid of duplicate lines 
 DataView dv = table.DefaultView;
 table = dv.ToTable(true, new string[] { "name", "code" });

  At this time table  There is only name , code No duplicate of the two lines, if necessary id Values are 

 table = dv.ToTable(true, new string[] { "id","name", "code" });// The first 1 A parameter true  Enable to repeat, similar distinct

If you have 1 set of data (id is not the only 1 field)


id   name   code
     zhang 3    123
     li 4    456
     zhang 3    456
    zhang 3     123

You get it by the way above


id   name   code
     zhang 3    123
     li 4    456
     zhang 3    456

What if you want to filter only the data that id name code does not allow for duplication?


table = dv.ToTable(true, new string[] { "name"});

Get:


name 

 zhang 3  

 li 4  

But what I want is to repeat only one of the columns, name, and show the other columns

The desired results are:


 id   name   code

1     zhang 3    123
2     li 4    456

How do you do that? The following method can be, there may be a better method, hope you give more advice


#region  delete DataTable Repeat the column, similar distinct
         /// <summary>   
         ///  delete DataTable Repeat the column, similar distinct   
         /// </summary>   
         /// <param name="dt">DataTable</param>   
         /// <param name="Field"> The field name </param>   
         /// <returns></returns>   
         public static DataTable DeleteSameRow(DataTable dt, string Field)
         {
             ArrayList indexList = new ArrayList();
             //  Find the row index to drop    
             for (int i = 0; i < dt.Rows.Count - 1; i++)
             {
                 if (!IsContain(indexList, i))
                 {
                     for (int j = i + 1; j < dt.Rows.Count; j++)
                     {
                         if (dt.Rows[i][Field].ToString() == dt.Rows[j][Field].ToString())
                         {
                             indexList.Add(j);
                         }
                     }
                 }
             }
             //  Delete rows according to the list of indexes to be deleted    
             for (int i = indexList.Count - 1; i >= 0; i--)
             {
                 int index = Convert.ToInt32(indexList[i]);
                 dt.Rows.RemoveAt(index);
             }
             return dt;
         }

         /// <summary>   
         ///  Determines if the array exists    
         /// </summary>   
         /// <param name="indexList"> An array of </param>   
         /// <param name="index"> The index </param>   
         /// <returns></returns>   
         public static bool IsContain(ArrayList indexList, int index)
         {
             for (int i = 0; i < indexList.Count; i++)
             {
                 int tempIndex = Convert.ToInt32(indexList[i]);
                 if (tempIndex == index)
                 {
                     return true;
                 }
             }
             return false;
         }
         #endregion


Related articles: