问题
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.
回答1:
Actually, ADO.NET added a(n apparently not well known) feature that allows you to create a new table containing the distinct entries from an existing table. Here's how it works: ..... .....
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/ed9c6a6a-a93e-4bf5-a892-d8471b84aa3b/
回答2:
Could you use a linq group as an alternative? I couldn't say how much quicker it would be but I dare say it will be well optimised.
var result = from x in source.Table.AsEnumerable()
group x by new { id = x.Field<int>("ID"), Name = x.Field<string>("Name"), Date = x.Field<DateTime>("Date") }
into groupedResults
select groupedResults.Key;
来源:https://stackoverflow.com/questions/6856632/how-to-imporove-the-performance-of-my-method-created-to-remove-duplicates-from-a