DataTable 중복된 값 제거
※ DataTable에 존재하는 Data중 중복된 값 제거하기
#region Row, Row를 비교한다.
public static bool RowEqual(object[] Values, object[] OtherValues)
{
if (Values == null) return false;
for (int i = 0; i < Values.Length; i++)
{
if (!Values[i].Equals(OtherValues[i])) return false;
}
return true;
}
#endregion
#region 데이터테이블을 distinct한다.
public static DataTable Distinct(DataTable Table, DataColumn[] Columns)
{
DataTable dt = null;
string sort = string.Empty;
try
{
if (Table != null)
{
dt = new DataTable("Distinct");
for (int i = 0; i < Columns.Length; i++)
{
dt.Columns.Add(Columns[i].ColumnName, Columns[i].DataType);
sort += Columns[i].ColumnName + ",";
}
object[] currentrow = null;
object[] previousrow = null;
DataRow[] sortedrows = Table.Select(string.Empty, sort.Substring(0, sort.Length - 1));
dt.BeginLoadData();
foreach (DataRow row in sortedrows)
{
currentrow = new object[Columns.Length];
for (int i = 0; i < Columns.Length; i++)
{
currentrow[i] = row[Columns[i].ColumnName];
}
if (!RowEqual(previousrow, currentrow))
{
dt.LoadDataRow(currentrow, true);
}
previousrow = new object[Columns.Length];
for (int i = 0; i < Columns.Length; i++)
{
previousrow[i] = row[Columns[i].ColumnName];
}
}
dt.EndLoadData();
}
}
catch (Exception ex) // 프로그램에서 예상하지 못한 Exception을 처리합니다.
{
throw ex;
}
finally // 더 이상 사용하지 않는 자원을 해제합니다.
{
}
return dt;
}