blah blah blah is here! blah blah » Close

up0down
link

Hi,
I'm trying to find in a dataset if the next value is the same and if it is take its value in another var.

ex.
abc 100 200
abc 150 100
abc 100 250
abx 100 230
abv 100 200
abv 150 100
the data set is ordered...
and i want to have

RESULT
abc 350 550
abx 100 230
abv 250 300

I was trying in this way... but... :(

for (int i = 0; i < dsLog.Tables[0].Rows.Count; i++)
{
cb = System.Convert.ToInt32(dsLog.Tables[1].Rows[i][0].ToString());
cs = System.Convert.ToInt32(dsLog.Tables[2].Rows[i][0].ToString());
fat = dsLog.Tables[0].Rows[i][0].ToString();

if (fat == dsLog.Tables[0].Rows[i+1][0].ToString())
{
cb += System.Convert.ToInt32(dsLog.Tables[1].Rows[i+1][0].ToString());
cs += System.Convert.ToInt32(dsLog.Tables[2].Rows[i+1][0].ToString());
fat = dsLog.Tables[0].Rows[i+1][0].ToString();

}

funcPutToListView(fat, cs.ToString(), cb.ToString());

}


Can you help me?

Daniel

last answered one year ago

1 answers

up0down
link

Try this:

for (int i = 0; i < dsLog.Tables[0].Rows.Count; i++)
{
int cbCurrent = System.Convert.ToInt32(dsLog.Tables[1].Rows[i][0].ToString());
int csCurrent = System.Convert.ToInt32(dsLog.Tables[2].Rows[i][0].ToString());
string fatCurrent = dsLog.Tables[0].Rows[i][0].ToString();

if (i == 0)
{
cb = cbCurrent;
cs = csCurrent;
fat = fatCurrent;
}
else if (fat == fatCurrent)
{
cb += cbCurrent;
cs += csCurrent;
}
else
{
funcPutToListView(fat, cs.ToString(), cb.ToString());
fat = fatCurrent;
cb = cbCurrent;
cs = csCurrent;
}

if (i == dsLog.Tables[0].Rows.Count - 1) funcPutToListView(fat, cs.ToString(), cb.ToString());
}

thanks vulpes, infact i used something like this... but... don't know... i don't like its way of finding... is cause i'm using a dataset? i refer to the check of the first and the last element of the array.

vulpes
17279

Whether you're using a dataset or anything else, I don't think it's possible to devise an algorithm for this kind of collation which doesn't treat the first and last elements specially, though sometimes its done outside the loop rather than within it.

Feedback