将数据表放到内存中进行操作,可以极大的提高效率。
/// <summary> /// 删除DataTable重复列,类似distinct /// </summary> /// <param name="dt">DataTable</param> /// <param name="Field">字段名</param> /// <returns></returns> public DataTable DeleteSameRow(DataTable Table, string Field) { DataTable dt = Table.Copy(); 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; }
标签:Rows,indexList,删除,Distinct,数据库,int,dt,DataTable From: https://www.cnblogs.com/kjgagaga/p/16733687.html