#region 删除DataTable重复列,类似distinct |
/// <summary> |
/// 删除DataTable重复列,类似distinct |
/// </summary> |
/// <param name="dt">DataTable</param> |
/// <param name="Field">字段名</param> |
/// <returns></returns> |
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 |