开发者

c# how to get last time in foreach statement? [duplicate]

This question already has answers here: 开发者_运维问答 Closed 10 years ago.

Possible Duplicate:

Foreach loop, determine which is the last iteration of the loop

foreach (DataRowView row in orderedTable.DefaultView)
{
    if(lasttime) do-something;
}

orderedtable is a datatable

does anyone know how to find out whether we are on the last foreach iteration? please keep in mind that i do have duplicates in orderedtable


The correct method that works in all cases is to use the IEnumerator<T> directly:

using (var enumerator = orderedTable.DefaultView.GetEnumerator())
{
    if (enumerator.MoveNext())
    {
        bool isLast;
        do
        {
            var current = enumerator.Current;
            isLast = !enumerator.MoveNext();
            //Do stuff here
        } while (!isLast);
    }
}

This method works even if your collection doesn't have a Count property, and even if it does, this method will be more efficient if the Count property is slow.


The foreach construct does not know such a thing, since it applies equally to unbounded lists. It just has no way of knowing what is a last item.

You can iterate the manual way as well, though:

for (int i = 0; i < orderedTable.DefaultView.Count; i++) {
    DataRowView row = orderedTable.DefaultView[i];
    if (i == orderedTable.DefaulView.Count - 1) {
        // dosomething
    }
}


An alternative approach which I don't think anyone posted. This works well if you don't know the count ahead of time.

DataRowView lastRow;
foreach (DataRowView row in orderedTable.DefaultView)
{
    // Do something...

    lastRow = row;
}

if (lastRow != null)
{
  // Do something with last row
}


You will have to use a regular for loop if you want to have different behavior on the last item.

for (int i = 0; i < orderedTable.DefaultView.Count; i++)
{
    //do stuff
    if (i == orderedTable.DefaultView.Count - 1)
    {
        //do additional special stuff
    }
}

It's worth noting that "the other Skeet" has an implementation for a "smart enumerable" which supports a Last property. See the article here: http://msmvps.com/blogs/jon_skeet/archive/2007/07/27/smart-enumerations.aspx

With this you could write something like this (I might get the details wrong, haven't tried it out myself):

foreach (SmartEnumerable<DataRowView> item in new SmartEnumerable<DataRowView>(orderedTable.DefaultView))
{
    DataRowView row = item.Value;
    if(item.IsLast)
    {
       ///do special stuff
    }
}


Instead of using foreach get the IEnumerator. If MoveNext returns null the previous was the last one.

for would work too of course.


You could possibly do something like the following:

foreach (DataRowView row in orderedTable.DefaultView)
{
    if(row == orderedTable.DefaultView.Last()) do-something;
}

But its pretty inefficient.


If you're concerned in keeping track of your iteration, why not use a for instead or a foreach? Then you can simply do the following:

for(int i = 0; i<orderedTable.DefaultView.Count-1;i++)
{
   if(i==orderedTable.DefaultView.Count-1)
   {
      // Last Row
   }
}


The code you have posted is identical to:

if (orderedTable.DefaultView.Rows.Count > 0)
    do-something;

If that does not do what you want, you will have to explain what lastTime does.


If I understand your question, does this help?

int lastrow = 0;
foreach (DataRow row in table.Rows) // Loop over the rows.
{
    lastrow++;
    // Do Something
    if (lastrow == (table.Rows.Count - 1))
    {
        // Do something else
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜