I am trying to iterate over a DataTable and get values from a specific column. So far I have a for loop skeleton.
foreach (var row in currentTable.Rows) { var valueAtCurrentRow = row[0]; }
This does not work as I expected. I get a compiler error when I try to make row[0] , with the message: "Cannot apply indexing from [] to an expression of type Object". But row should not be an object, it is a DataRow .
To fix this, I changed the foreach loop to the following:
foreach (DataRow row in currentTable.Rows) { var valueAtCurrentRow = row[0]; }
Why is this necessary? Why can't C # infer the type row , as if I were trying to iterate over string[] , for example?
Michael hancock
source share