数据表 - foreach 行,除了第一行
我在我的应用程序中使用 DataTable
进行一些计算。我需要迭代除第一行之外的所有行。是否可以?
像这样的东西:
DataTable dt;
foreach (DataRow r in dt.Rows /*EXCEPT THE FIRST ONE*/)
{
//do something...
}
I am using a DataTable
for some calculations in my app. I need to do the iterate trough all the rows except the first one. Is it possible?
Something like:
DataTable dt;
foreach (DataRow r in dt.Rows /*EXCEPT THE FIRST ONE*/)
{
//do something...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LINQ 是您的朋友:
这里需要调用
Cast()
,因为DataTable.Rows
实现非泛型IEnumerable
和 linq 的扩展方法仅适用于IEnumerable
您还有另一种选择:
LINQ is your friend:
The call to
Cast()
is required here sinceDataTable.Rows
implements the non-genericIEnumerable
, and linq's extension methods are only available forIEnumerable<T>
You also have another option:
好吧,你已经得到答案了,但如果你不想使用 linq。检查表中行的索引:
Ok you got your answers but in case you donT want to use linq. Check the index of the row in the table:
这是一个快速而肮脏的
Here's a quick and dirty