Iterate collections

  • Thread starter Thread starter Antonio Budano
  • Start date Start date
A

Antonio Budano

Hi there,

How can I iterate through collections starting not from the first item but
from an offset?
I need to check if a dataset contains records that have some field values
that can conflict with some other records. So my code would be like the
following:

MyDataSet copyDataSet = new MyDataSet();
copyDataSet = myDataSet.Copy();
foreach(DataRow row in myDataSet.MyTable.Rows)
foreach(DataRow copyRow in copyDataSet.MyTable.Rows)
Do the records validation


the problem with this approach is that I will do checks twice, I need to
start iterating the copyDataSet.MyTable.Rows not from the first row but from
the row of myDataSet.MyTable.Rows + 1.

How can I achieve this?

Thanks
Antonio
 
use a for loop

for(int x = 10; x < ds.Tables[0].Rows.Count; x++){
for(int y = 5; y < ds.Tables[0].Columns.Count; y++){
ds.Tables[0].Rows[x][y] == . . .
}
}
 
Hi Antonio,

How about something like this?

int rowIdx = 0;
foreach (DataRow row in myDataSet.MyTable.Rows)
{
for (int r = rowIdx; r < copyDataSet.MyTable.Rows.Count; r++)
{
DataRow copiedRow = copyDataSet.MyTable.Rows[rowIdx];
// Do what you need
}
rowIdx ++;
}

-Joe
 
Why did I post this stupid question?
It is too late here in Italy to continue to work, I am going to bed.

Thank you for your answer, also to Eric.
Antonio
 
generally, don't like to mix methods for rolling through a
collection, so use a for in both cases, or neither...

how about...
DataRow row, copy;
for (int i=0; i < myDataSet.MyTable.Rows.Count; i++) {
row = myDataSet.MyTable.Rows;
for (int j=i+1; j<copyDataSet.MyTable.Rows.Count; j++) {
copy = copyDataSet.MyTable.Rows[j];
//do your stuff...
}
}

depending on the actions being taken, a DataView is probably
a better option in any case...
Hi Antonio,

How about something like this?

int rowIdx = 0;
foreach (DataRow row in myDataSet.MyTable.Rows)
{
for (int r = rowIdx; r < copyDataSet.MyTable.Rows.Count; r++)
{
DataRow copiedRow = copyDataSet.MyTable.Rows[rowIdx];
// Do what you need
}
rowIdx ++;
}

-Joe

Antonio Budano said:
Hi there,

How can I iterate through collections starting not from the first item but
from an offset?
I need to check if a dataset contains records that have some field values
that can conflict with some other records. So my code would be like the
following:

MyDataSet copyDataSet = new MyDataSet();
copyDataSet = myDataSet.Copy();
foreach(DataRow row in myDataSet.MyTable.Rows)
foreach(DataRow copyRow in copyDataSet.MyTable.Rows)
Do the records validation


the problem with this approach is that I will do checks twice, I need to
start iterating the copyDataSet.MyTable.Rows not from the first row but
from the row of myDataSet.MyTable.Rows + 1.

How can I achieve this?
 

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

Back
Top