DataTable - foreach Row, EXCEPT THE FIRST ONE

I am using DataTable for some calculations in my application. I need to iterate over all lines except the first. Is it possible?

Sort of:

 DataTable dt; foreach (DataRow r in dt.Rows /*EXCEPT THE FIRST ONE*/) { //do something... } 
+6
source share
3 answers

Well, you have the answers, but if you do not want to use linq. Check the row index in the table:

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

LINQ is your friend:

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

This requires calling Cast() , because DataTable.Rows implements a non-generic IEnumerable , and linq extension methods are only available for IEnumerable<T>

You also have another option:

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

It's quick and dirty here

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

Source: https://habr.com/ru/post/905918/


All Articles