public static DataTable Distinct(DataTable dt, string[] filedNames)
{
DataView dv = dt.DefaultView;
DataTable DistTable = dv.ToTable("Dist", true, filedNames);
return DistTable;
}
利用for循环遍历DataTable的数据行,利用DataTable.Select 方法判断是否重复,如果重复,则利用DataTable.Rows.RemoveAt(Index)删除重复的那一行。
public DataTable GetDistinctSelf(DataTable SourceDt, string filedName)
{
for (int i = SourceDt.Rows.Count - 2; i > 0; i--)
{
DataRow[] rows = SourceDt.Select(string.Format("{0}=‘{1}‘", filedName, SourceDt.Rows[i][filedName]));
if (rows.Length > 1)
{
SourceDt.Rows.RemoveAt(i);
}
}
return SourceDt;
}
public DataTable GetDistinctSelf(DataTable SourceDt, string filedName)
{
for (int i = SourceDt.Rows.Count - 2; i > 0; i--)
{
string title = SourceDt.Rows[0][filedName].ToString();
for (int j = i + 1; j > 0; i--)
{
if (SourceDt.Rows[j][filedName].ToString() == title)
{
SourceDt.Rows.RemoveAt(i);
}
}
}
return SourceDt;
}