arun_mrk Posted April 1, 2003 Posted April 1, 2003 duplicate rows in dataset? I have a dataset which will have duplicate rows and some time it might not have duplicate rows. I would like to write a function to check for 'duplicate row ' in dataset and 'return true' if duplicate rows are found else i want to 'return false' How can i do it? Quote
Moderators Robby Posted April 1, 2003 Moderators Posted April 1, 2003 Do you want to eliminate the duplicates or are you doing something else? Quote Visit...Bassic Software
*Gurus* Derek Stone Posted April 1, 2003 *Gurus* Posted April 1, 2003 It would be far simpler if you used SQL: SELECT DISTINCT * FROM table Quote Posting Guidelines
arun_mrk Posted April 2, 2003 Author Posted April 2, 2003 I didn't build the dataset from the database. I built it based on the user inputs. It big Process to explain how the duplicate rows where added. But all that i can say is i have a dataset with many rows. I want to write a function to check if there are any duplicate rows if yes, the should return "false" else it should return "true". My only problem is how to check whether a duplicate row exists or not? Quote
*Experts* jfackler Posted April 2, 2003 *Experts* Posted April 2, 2003 (edited) Is there a field that, if you checked it, the entire row would definitely be duplicated? If so, the rows of data are a collection and as such you can do a for each and check for a match to some value, then if there's a match assign some value true. Dim dr As DataRow Dim key As Integer = 0 'in your database, the column that holds 'the key value who's row your looking to match Dim int As Integer =0 Dim bc As Integer = 0 For Each dr In dataset1.Tables("MyTable").Rows 'array of datarows If CStr(dr.Item(key)) Is "SomeValue" Then 'interogates each datarow for the 'key value "SomeValue" who's row you want to edit bc = int ' sets binding context to row that holds key value End If int += 1 'counter for rows...next row Next You may want to create an array to hold the matches or, if you're certain you want to delete that row, then add this after the if statement in the above code: bc = int ' sets binding context to row that holds key value End If dataset1.Tables("MyTable").Rows(bc).Delete() int += 1 'counter for rows...next row Next And it will delete all those that match in the dataset. Edited April 2, 2003 by jfackler Quote
arun_mrk Posted April 2, 2003 Author Posted April 2, 2003 i have some doubts in the above code. You have written " i.item" in the 'if' part. What is 'i'? and what should i put(subsitute) in "some value". In my dataset i have a key column(name code) if its same than it means the row is similiar(duplicate). Quote
*Experts* jfackler Posted April 2, 2003 *Experts* Posted April 2, 2003 Sorry, i should have been dr take a look back. Quote
*Experts* jfackler Posted April 2, 2003 *Experts* Posted April 2, 2003 The datarows aren't an array either, they're a collection. cstr(dr.item(key)) should return the value as a string of the value held in the first column (0) of your dataset. You can change that to any column as an integer that holds your Key (in your case, the name code). Quote
arun_mrk Posted April 2, 2003 Author Posted April 2, 2003 i understand clearly about the dr.Item(key). In your code u had used 'somevalue', what do u expect me to substute in that place. dr.Item(key) will have all my 'code' in the dataset, u want me to compare these code with 'what value'? actually i want to find the duplicate codes? Quote
*Experts* jfackler Posted April 2, 2003 *Experts* Posted April 2, 2003 Replace the "some value" with the value of your name code. If that's an integer, change your cstr to cint....etc. You can create a collection (beyond this discussion) and add the datarows that are matches at the same place I added the delete statement in the second code snippet. That will give you a collection of rows that are duplicate rows. Quote
*Experts* jfackler Posted April 2, 2003 *Experts* Posted April 2, 2003 (edited) Answered in your original post. Look there. [edit]I merged the two threads[/edit] Edited April 2, 2003 by Robby Quote
*Experts* Nerseus Posted April 2, 2003 *Experts* Posted April 2, 2003 There's an easier method to determine dupes in a DataSet. It might not be as speedy as other methods, but it's easy to understand AND code. First, create a DataRow array using the DataTable's Select method with a sort on the Primary Key or Keys (depending on whether your table's "duplicates" are based on one column or more than one). Loop through each row starting at the second row and compare the same column(s) to the previous row. If they match, then they're duplicates. Here's some sample C# code (untested). I assume the PrimaryKeyCol is a string (hence the ToString method below). If your primary key column is an ID, cast the column as an int or whatever you need: DataRow[] dupes = ds.Tables["DupeTable"].Select(String.Empty, "PrimaryKeyCol"); for(int i=1; i<dupes.Length; i++) if(dupes[i]["PrimaryKeyCol"].ToString() == dupes[i-1]["PrimaryKeyCol"].ToString()) // There's a dupe at index i -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.