DataSet scan efficiency

M

Maya

Hello,

I have a dataset i need to insert new datarow in if the row is new and
doesn't exist in the dataset, other wise i will only need to update the
existing row.

for example, dataset with "ID" and "Name" fields can have a new row
added if the row's ID is not already in the dataset.

Any idea what the most efficient way (light performance) is to achieve
this task? maybe using "foreach" would do the job but i think this
wouldn't be very efficient as this operation needs to be done every
couple of seconds and requires fast execution time.

Thanks,

Maya.
 
C

Cor Ligthert [MVP]

Maya,

A foreeach takes on most computers a part of a nanosecond so be not afraid
for that.

However you don't need the foreach. You can use one of the two finds, both
do what you want, however I think that in your case the datatable
rowcollection find is the best.

http://msdn.microsoft.com/library/d...systemdatadatarowcollectionclassfindtopic.asp

If you find it, than you change the field
If you don't find it, you add a new row.

I hope this helps,


Cor
 
M

Maya

Thanks Cor,

Just out of curiosity, would "find" be also a good solution to be used
in this scenario:


2 datasets have to be merged, both have the same fields "ID" and
"value", primary is "ID"

when merging a duplicate "ID", the "value" field will have a specific
formula to be applied as following:

DS1: ID: "1", Value: "2"
DS2: ID: "1", Value: "4"
MergedDS: ID: "1", Value: "8"

The example above merge the 2 ID records into 1 record with a value of
multiplying the first ID value with the second one.

I don't know if merge method would allow to apply custom calculation
when duplicate is found, but would find also do the job in this case?

Thanks,

Maya
 
C

Cor Ligthert [MVP]

Maya,

A merge, merges all values as exist or not exist from the to import
datatable into the existing datatable.

In other words
1 a and 1 b
2 c
Will result in
1b
2c

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top