I have created a method to remove duplicates froma a DataView. I have not option to change the SQl query , so my only option is to modify the existing data retrieved from the Database in the DataView.
DataView data
Id, Name, Date
1, Paul, 12-05-2011
2, Mark, 12-05-2011
1, Paul, 12-05-2011
2, Mark, 12-05-2011
My method is:
private static void RemoveDuplicates(DataView source, string keyColumn)
{
DataRow[] dataRows = new DataRow[source.Table.Rows.Count];
source.Table.Rows.CopyTo(dataRows, 0);
var uniquePrimaryKeys = new List<Guid>(duplicateTable.Rows.Count);
foreach (DataRow row in duplicateTable.Rows)
{
if (uniquePrimaryKeys.Contains((Guid)row[keyColumn]))
source.Table.Rows.Remove(row);
else
uniquePrimaryKeys.Add((Guid)row[keyColumn]);
}
}
I wonder if there is a better method to achieve the same result but faster.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…