[C#] datable删除重复咧 →→→→→进入此内容的聊天室

来自 , 2020-03-18, 写在 C#, 查看 117 次.
URL http://www.code666.cn/view/a3d06db1
  1. #region 删除DataTable重复列,类似distinct
  2.         /// <summary>  
  3.         /// 删除DataTable重复列,类似distinct  
  4.         /// </summary>  
  5.         /// <param name="dt">DataTable</param>  
  6.         /// <param name="Field">字段名</param>  
  7.         /// <returns></returns>  
  8.         public static DataTable DeleteSameRow(DataTable dt, string Field)
  9.         {
  10.             ArrayList indexList = new ArrayList();
  11.             // 找出待删除的行索引  
  12.             for (int i = 0; i < dt.Rows.Count - 1; i++)
  13.             {
  14.                 if (!IsContain(indexList, i))
  15.                 {
  16.                     for (int j = i + 1; j < dt.Rows.Count; j++)
  17.                     {
  18.                         if (dt.Rows[i][Field].ToString() == dt.Rows[j][Field].ToString())
  19.                         {
  20.                             indexList.Add(j);
  21.                         }
  22.                     }
  23.                 }
  24.             }
  25.             indexList.Sort();
  26.             // 排序
  27.             for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行  
  28.             {
  29.                 int index = Convert.ToInt32(indexList[i]);
  30.                 dt.Rows.RemoveAt(index);
  31.             }
  32.             return dt;
  33.         }
  34.  
  35.         /// <summary>  
  36.         /// 判断数组中是否存在  
  37.         /// </summary>  
  38.         /// <param name="indexList">数组</param>  
  39.         /// <param name="index">索引</param>  
  40.         /// <returns></returns>  
  41.         public static bool IsContain(ArrayList indexList, int index)
  42.         {
  43.             for (int i = 0; i < indexList.Count; i++)
  44.             {
  45.                 int tempIndex = Convert.ToInt32(indexList[i]);
  46.                 if (tempIndex == index)
  47.                 {
  48.                     return true;
  49.                 }
  50.             }
  51.             return false;
  52.         }
  53.         #endregion

回复 "datable删除重复咧"

这儿你可以回复上面这条便签

captcha