1. 使用 DataView
// create a dv from the source dt DataView dv = new DataView(dt); // set the output columns array of the destination dt string[] strColumns = {"NodeID", "Title", "Url"}; // true = yes, i need distinct values. dt = dv.ToTable(true, strColumns);
2. 使用linq groupby
protected DataTable Dedup(DataTable dt, params string [] keys) { DataTable result = null; switch(keys.Length) { case 1: result = dt.AsEnumerable().GroupBy(row => new{ c0 = row[keys[0]] }).Select(g=>g.First()).CopyToDataTable(); break; case 2: result = dt.AsEnumerable().GroupBy(row => new{ c0 = row[keys[0]], c1 = row[keys[1]] }).Select(g=>g.First()).CopyToDataTable(); break; case 3: result = dt.AsEnumerable().GroupBy(row => new{ c0 = row[keys[0]], c1 = row[keys[1]], c2=row[keys[2]] }).Select(g=>g.First()).CopyToDataTable(); break; } return result; }