Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...