foreach循环从row1开始 [英] foreach loop starts from row1

查看:319
本文介绍了foreach循环从row1开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DataTable中有10行。现在我想使用foreach循环插入记录。

喜欢这个



  foreach (DataRow dr dt.Rows)
{

}





但我不想插入第0行。



我想从第1行插入词语。



我知道使用for循环的逻辑但是我想使用foreach。



任何人都可以给我建议使用foreach来修复这个问题。 。

解决方案

foreach循环始终适用于您提供它的Collection上的所有元素,因此没有机制可以跳过第一个(或任何其他元素) )除了使用 break 完全退出循环。



有两种方法可以错过第一个元素:

1)将列表复制到新的集合中并删除第一个元素。

2)在foreach之外添加一个bool:

< pre lang =cs> bool skip = true ;
foreach (DataRow row in dt.Rows)
{
if (!skip)
{
...
}
skip = ;
}


用于循环,从1开始到你的dt.rows.count


I have a 10 rows in my DataTable. Now i want to insert records using foreach loop.
like this

foreach(DataRow dr dt.Rows)
{

}



But i don''t want to insert row 0.

I want to insert from row 1 onwords.

I know the logic using for loop but i want using foreach.

Can any one give me suggestion to fix the possition using foreach only...

解决方案

A foreach loop always works on all the elements on the Collection you feed it, so there is no mechanism to "skip" the first (or any other element) other than using break to exit the loop completely.

There are two ways to miss out the first element:
1) Copy the list into a new collection and remove the first element.
2) Add a bool outside the foreach:

bool skip = true;
foreach (DataRow row in dt.Rows)
    {
    if (!skip)
        {
        ...
        }
    skip = false;
    }


use for loop which start from 1 to your dt.rows.count


这篇关于foreach循环从row1开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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