我已经使用c#将数据从XML文件读入DataSet,而不是想要识别该集合中的重复(完全相同)行。 我试过这种分组,这很有效!
var d= from r1 in table.AsEnumerable()
group r1 by new
{
t0 = r1[0],
t1 = r1[1],
t2 = r1[2],
t3 = r1[3],
t4 = r1[4],
t5 = r1[5],
t6 = r1[6],
t7 = r1[7],
t8 = r1[8],
}
into grp
where grp.Count() > 1
select grp;
但是数据列的数量可能不同,因此我不能像上面那样在查询中应用静态分组。我必须动态生成分组数组吗?
我不想删除dublicate,我只是想找到它们!
答案 0 :(得分:5)
var rows = table.AsEnumerable();
var unique = rows.Distinct(DataRowComparer.Default);
var duplicates = rows.Except(unique); // , DataRowComparer.Default);
答案 1 :(得分:-1)
试试这个
DataTable dt = TableName;
dt = oPerformance.RemoveDuplicateRows(dt, ColToCompareAndRemove);
这是函数RemoveDuplicateRows
public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();
//Add list of all the unique item value to hashtable, which stores combination of key, value pair.
//And add duplicate item value in arraylist.
foreach (DataRow drow in dTable.Rows)
{
if (hTable.Contains(drow[colName]))
duplicateList.Add(drow);
else
hTable.Add(drow[colName], string.Empty);
}
//Removing a list of duplicate items from datatable.
foreach (DataRow dRow in duplicateList)
dTable.Rows.Remove(dRow);
//Datatable which contains unique records will be return as output.
return dTable;
}