C# 50000条数据的DataTable怎么快速找到完全重复的两行
时间: 2024-03-03 09:49:50 浏览: 12
可以使用以下代码实现:
```csharp
private List<DataRow[]> FindDuplicateRows(DataTable table)
{
List<DataRow[]> duplicateRows = new List<DataRow[]>();
HashSet<string> hashSet = new HashSet<string>();
foreach (DataRow row in table.Rows)
{
string rowString = string.Join(",", row.ItemArray);
if (hashSet.Contains(rowString))
{
DataRow[] duplicateRowPair = new DataRow[2];
duplicateRowPair[0] = row;
duplicateRowPair[1] = table.Select().FirstOrDefault(r => string.Join(",", r.ItemArray) == rowString);
duplicateRows.Add(duplicateRowPair);
}
else
{
hashSet.Add(rowString);
}
}
return duplicateRows;
}
```
这段代码会遍历 DataTable 中的每一行数据,并将每一行的数据转换为字符串,然后使用 HashSet 存储已经出现过的字符串。当遍历到某一行时,如果该行的字符串已经在 HashSet 中出现过,则说明该行与之前的某一行完全相同,此时可以将这两行存储下来。如果该行的字符串没有在 HashSet 中出现过,则将该行的字符串添加到 HashSet 中。最后,返回所有完全重复的行的列表。