C# 去除DataTable表中重複行

去除DataTable表中重複行有兩種方法:
sql

1、利用sql語句的distinct 關鍵字
如:select distinct * from table_name;
2、利用DataView.ToTable()方法
1.DataView.ToTable()c#

根據現有DataView中的行,建立並返回一個新的DataTable。數組

2.DataView.ToTable(String)ide

根據現有DataView中的行,建立並返回一個新的DataTable。參數String爲返回的DataTable的名稱,輸出的表與輸入表的列相通,不可自定義。spa

3.DataView.ToTable(Boolean,String[]).net

根據現有DataView中的行,建立並返回一個新的DataTable。參數Boolean若是爲true,則去重,爲false時不去重,且默認爲false。code

可自定義返回的列,數組String[]爲顯示返回列的集合。blog

例子:排序

//去掉重複行DataTable dt = db.GetDataTable("select * from 表名"); //得到datatableDataView dv = dt.DefaultView;table = dv.ToTable(true, new string[] { "name", "code" });

public DataTable GetDataTable(string strSql)
{
try
{
//DataSet dataSet = new DataSet();
//SqlDataAdapter adapter = new SqlDataAdapter(strSql, _Connection);
//adapter.Fill(dataSet);
//return dataSet.Tables[0];
DataTable dt= new DataTable ();
SqlDataAdapter adapter = new SqlDataAdapter(strSql, _Connection);
adapter.Fill(dt);
return dt;
}
catch
{
return null;
}
}
索引

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

若是有一組數據(id不是惟一字段)

name code
張三 123
李四 123
張三 456
張三 123

經過上面的方法獲得

name code
張三 123
李四 123
張三 456

4.DataView.ToTable(String,Boolean,String[])

根據現有DataView中的行,建立並返回一個新的DataTable。第一個參數string用來定義返回表的名稱。

注意:想要的結果是隻針對其中的一列去重,還要顯示其餘的列,怎麼作

#region 刪除DataTable重複列,相似distinct
        ///
        /// 刪除DataTable重複列,相似distinct   
        ///
        ///DataTable
        ///字段名
        ///
        public static DataTable DeleteSameRow(DataTable dt, string Field)
        {
            ArrayList indexList = new ArrayList();
            // 找出待刪除的行索引   
            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);
                        }
                    }
                }
            }
            indexList.Sort();
 // 排序
            for (int i = indexList.Count - 1; i >= 0; i--)// 根據待刪除索引列表刪除行  
            {
                int index = Convert.ToInt32(indexList[i]);
                dt.Rows.RemoveAt(index);
            }
            return dt;
        }    /// <summary>   
    /// 判斷數組中是否存在   
    /// </summary>   
    /// <param name="indexList">數組</param>   
    /// <param name="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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

借鑑文章:
一、C# DataTable去重,根據列名去重保留其餘列
原文連接:https://blog.csdn.net/qq_23502409/article/details/75269221
二、C# 中怎樣去除DataTable表裏面的重複行
原文連接:https://blog.csdn.net/u010892197/article/details/50310907
三、c# DataView.ToTable() 方法 去除表中的重複項
原文連接:https://blog.csdn.net/JYL15732624861/article/details/61422332


注意:好比:DataTable dt=db.GetDataTable(「select * from 表名」); //得到datatableDataView dv = new DataView(dt);//對dt這個表建立一個視圖(注意:這裏的DataView只能對一個datatable進行建立視圖)DataTable dt2 = dv.ToTable(true, 「name「,」age「,」sex」);//true:true 去除重複,false 不去除; "name「,「age」,「sex」:表示須要顯示的字段,特別注意:須要將每一個欄位用雙引號括起來,否則會報錯:列「name,age,sex」不屬於基礎表————————————————

相關文章
相關標籤/搜索