Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I've never done this as yet with VB.Net but now find I might have to....

 

If I have dataset1 that contains freshly imported data from a corp system and dataset2 that contains previously imported and actioned data, is there an efficient way to to search dataset2 for any record that exists in dataset1 so that these records will be dropped as they have previously been actioned upon :confused:

My website
  • Leaders
Posted

My first thought is to loop through the rows of dataset2 and add there ID's to a comma-delimited list. You could then use the RowFilter on DataSet1 along with the "IN" operator to filter down to either the rows you want to delete or negate it and get the rows you want to keep. If you use a StringBuilder the first part should be quick, but I have been unable to really determine just how quick applying rowfilters are thus far.

 

Note: Just to clarify, I'm following your lead and using the term Dataset loosely. I assume the context will tell you above whether I'm talking DataTable or DataView.

--tim
Posted

Thnx chaps...enligthening:)

 

Mehyar, this will be of use I feel at some later stage, but what I'm trying to achieve is this:

 

Dataset1 contains the latest import

Dataset2 contains an ever growing collection of previous imports

 

I need to end up with Dataset1 only containing records that do not exist in Dataset2. These would be actioned and then appended to Dataset2 ready for the next run.

 

I'm currently building this into an object setup whereby the object containing Dataset1 will have a method to search an object containing Dataset2. This would mean calling object with Dataset1's MoveNext method then do the search again.

 

Not sure if this is the most efficient way:)

My website
Posted
Hog execuse me for the question but does your dataset contain many tables or only one???
Dream as if you'll live forever, live as if you'll die today
  • *Experts*
Posted

I'm not sure what you're looking for in terms of duplicates so this may or may not help. Assuming you have one column in both datasets and you can use that one column as a duplicate check (you can expand this to multiple columns fairly easily):

DataSet ds1; // ds1 contains the original records
DataSet ds2; // ds2 contains all records, including potential dupes
DataRow[] dupes; // An array of duplicate rows

foreach(DataRow row in ds1.Tables["Table1"].Rows)
{
   // Get the unique ID from the original dataset
   int dupeID = row["UniqueColID"];

   dupes = ds2.Tables["Table1"].Select("UniqueColID = " + dupeID.ToString());
   for(int i=dupes.Length-1; i>=0; i--) dupes[i].Delete();
}

 

That loops through each row in the original dataset and finds duplicates in the new dataset. All matches found will be deleted.

 

You can do this in one shot if you use XSLT (XML transformations), which is like a language in and of itself. But it requires more work and might not be as readable, though might be faster.

 

Of course, I like to use actual SQL tables for this kind of thing. Insert all the rows from the new dataset, with potential dupes, into a scratch table (a table only used for this process). Then SELECT out DISTINCT rows into the real table. Or, delete the duplicates (delete from scratch table where ID's match those in the original table), then insert the rows into the real table.

 

-Nerseus

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...