C# DataTable - foreach Row,除了第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8852863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:47:50  来源:igfitidea点击:

DataTable - foreach Row, EXCEPT FIRST ONE

c#.netdatatableiterationdatarow

提问by user1080533

I am using a DataTablefor some calculations in my app. I need to do the iterate trough all the rows except the first one. Is it possible?

DataTable在我的应用程序中使用 a进行一些计算。我需要对除第一行以外的所有行进行迭代。是否可以?

Something like:

就像是:

DataTable dt;

foreach (DataRow r in dt.Rows /*EXCEPT THE FIRST ONE*/)
{
    //do something...
}

采纳答案by Orkun Ozen

Ok you got your answers but in case you donT want to use linq. Check the index of the row in the table:

好的,您得到了答案,但以防万一您不想使用 linq。检查表中行的索引:

            foreach (DataRow row in m_dtMatrix.Rows)
            {
                if (m_dtMatrix.Rows.IndexOf(row) != 0)
                {
                    ...
                }
            }

回答by Adi Lester

LINQ is your friend:

LINQ 是你的朋友:

DataTable dt;
foreach (DataRow r in dt.Rows.Cast<DataRow>().Skip(1))
{
    //do something...
}

The call to Cast()is required here since DataTable.Rowsimplements the non-generic IEnumerable, and linq's extension methods are only available for IEnumerable<T>

Cast()此处需要调用 ,因为DataTable.Rows实现了非泛型IEnumerable,并且 linq 的扩展方法仅适用于IEnumerable<T>

You also have another option:

您还有另一个选择:

DataTable dt;
foreach (DataRow r in dt.AsEnumerable().Skip(1))
{
    //do something...
}

回答by Matthew

Here's a quick and dirty

这是一个快速而肮脏的

DataTable dt;

bool isFirst = true;

foreach (DataRow r in dt.Rows /*EXCEPT THE FIRST ONE*/)
{
    if( isFirst ) {
        isFirst = false;
        continue;
    }
    //do something...
}