How to imporove the performance of my method created to remove duplicates from a DataView?

情到浓时终转凉″ 提交于 2019-12-13 03:55:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!