How do I get the row interation of a foreach statement?

J

jmDesktop

I have this:

foreach (DataRow row in myDS.Tables["myTable"].Rows)
{
int ctr=0; //counter for child node

//create a parent node for each new row
trvRetrieved.Nodes.Add(new
TreeNode(myIdNumber));

//children of that parent row
trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myPerson));
trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myAddress));

ctr++;

}

Is there a way to get the current row iteration number I am on instead
of using the by hand int counter?

Thank you.
 
J

Jon Skeet [C# MVP]

I have this:

 foreach (DataRow row in myDS.Tables["myTable"].Rows)
{
                        int ctr=0; //counter for child node

                        //create a parent node for each new row
                        trvRetrieved.Nodes.Add(new
TreeNode(myIdNumber));

                        //children of that parentrow
                        trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myPerson));
                        trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myAddress));

                        ctr++;

}

Is there a way to get the current row iteration number I am on instead
of using the by hand int counter?

I have a "SmartEnumerable" which wraps any other IEnumerable<T> to
provide the index, along with whether the entry is first and/or last.
See http://www.yoda.arachsys.com/csharp/miscutil/usage/smartenumerable.html
for details.
It only works with generic IEnumerables, which may be an issue (IIRC,
the Rows property is nongeneric) but you could always duplicate the
code for a nongeneric version.

Jon
 
P

Pavel Minaev

I have this:

 foreach (DataRow row in myDS.Tables["myTable"].Rows)
{
                        int ctr=0; //counter for child node

                        //create a parent node for each new row
                        trvRetrieved.Nodes.Add(new
TreeNode(myIdNumber));

                        //children of that parentrow
                        trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myPerson));
                        trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myAddress));

                        ctr++;

}

Is there a way to get the current row iteration number I am on instead
of using the by hand int counter?

LINQ'ish solution:

foreach (var t in dataTable.Select((row, index) => new { Row = row,
Index = index })
{
// Use t.Row and t.Index as needed.
...
}

If necessary, you could speed that up a bit by using
KeyValuePair<DataRow, int> rather than anonymous type (the former is a
value type, the latter is a reference type).
 
G

Göran Andersson

jmDesktop said:
I have this:

foreach (DataRow row in myDS.Tables["myTable"].Rows)
{
int ctr=0; //counter for child node

//create a parent node for each new row
trvRetrieved.Nodes.Add(new
TreeNode(myIdNumber));

//children of that parent row
trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myPerson));
trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myAddress));

ctr++;

}

Is there a way to get the current row iteration number I am on instead
of using the by hand int counter?

Thank you.

No, the enumerator doesn't expose the index of the current item.

You can rewrite the loop to use an index instead of an enumerator:

DataRowCollection rows = myDS.Tables["myTable"].Rows;
for (int ctr = 0; ctr < rows.Count; ctr++) {
DataRow row = rows[ctr];

//create a parent node for each new row
trvRetrieved.Nodes.Add(new TreeNode(myIdNumber));

//children of that parent row
trvRetrieved.Nodes[ctr].Nodes.Add(new TreeNode(myPerson));
trvRetrieved.Nodes[ctr].Nodes.Add(new TreeNode(myAddress));

}
 

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