C#中如何获得最后一次在foreach语句? [英] c# how to get last time in foreach statement?

查看:130
本文介绍了C#中如何获得最后一次在foreach语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/7476174/foreach-loop-determine-which-is-the-last-iteration-of-the-loop">Foreach环,确定哪个是环的的最后一次迭代

 的foreach(DataRowView的行orderedTable.DefaultView)
{
    如果(lasttime)做出头;
}
 

orderedtable是一个DataTable

没有人知道如何找出我们是否在最后的foreach循环?请记住,我确实有orderedtable重复

解决方案

的foreach 构造不知道这样的事情,因为它同样适用于无限名单。它只是没有办法知道是什么的最后的项目。

您可以重复手动方式为好,但:

 的for(int i = 0; I&LT; orderedTable.DefaultView.Count;我++){
    DataRowView的行= orderedTable.DefaultView [I]
    如果(ⅰ== orderedTable.DefaulView.Count  -  1){
        // 做一点事
    }
}
 

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 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
    }
}

这篇关于C#中如何获得最后一次在foreach语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆