zoukankan      html  css  js  c++  java
  • 删除DataTable重复列,只针对删除其中的一列重复的行(转)

    原文地址:http://www.cnblogs.com/wifi/archive/2013/02/19/2916565.html

    vs2005针对datatable已经有封装好的去重复方法:

    复制代码
    1 //去掉重复行
    2 DataView dv = table.DefaultView;
    3 table = dv.ToTable(true, new string[] { "name", "code" });
    4 
    5 此时table 就只有name、code无重复的两行了,如果还需要id值则
    6 
    7 table = dv.ToTable(true, new string[] { "id","name", "code" });//第一个参数true 启用去重复,类似distinct
    复制代码

    如果有一组数据(id不是唯一字段)

     

    复制代码
    id   name   code
    
    
    1    张三    123
    
    2    李四    456
    
    3    张三    456
    
    1   张三     123
    复制代码

     

    通过上面的方法得到

    复制代码
     id   name   code
    
    
    1    张三    123
    
    2    李四    456
    
    3    张三    456
    复制代码

     

    去重复去掉的仅仅是 id name code完全重复的行,如果想要筛选的数据仅仅是name不允许重复呢?

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

    得到:

    复制代码
    name 
     
    张三  
      
    李四  
    复制代码

    但是我想要的结果是只针对其中的一列name列 去重复,还要显示其他的列

    需要的结果是:

    复制代码
     id   name   code
    
    
    1    张三    123
    
    2    李四    456
    复制代码

    这个该怎么实现?下面的方法就可以,也许有更好的方法,希望大家多多指教

    复制代码
     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             // 根据待删除索引列表删除行   
    26             for (int i = indexList.Count - 1; i >= 0; i--)
    27             {
    28                 int index = Convert.ToInt32(indexList[i]);
    29                 dt.Rows.RemoveAt(index);
    30             }
    31             return dt;
    32         }
    33 
    34         /// <summary>   
    35         /// 判断数组中是否存在   
    36         /// </summary>   
    37         /// <param name="indexList">数组</param>   
    38         /// <param name="index">索引</param>   
    39         /// <returns></returns>   
    40         public static bool IsContain(ArrayList indexList, int index)
    41         {
    42             for (int i = 0; i < indexList.Count; i++)
    43             {
    44                 int tempIndex = Convert.ToInt32(indexList[i]);
    45                 if (tempIndex == index)
    46                 {
    47                     return true;
    48                 }
    49             }
    50             return false;
    51         }
    52         #endregion   
    复制代码

     

  • 相关阅读:
    java序列化进阶
    jQuery学习一(选择器)
    java poi 操作Excel常用方法总结
    mybatis批量foreach的使用
    java中关于try、catch、finally的总结
    MyEclipse使用指南(精简版)
    单例模式
    抽象工厂模式
    java.io.Serializable
    java.lang.string
  • 原文地址:https://www.cnblogs.com/zhang9418hn/p/2916837.html
Copyright © 2011-2022 走看看