Yes, normally that is what should happen.
If you need an explicit ordering of the rows I would suggest you sort them before iterating through them. Either by doing an OrderBy<>
linq type query or by using the Select
method on the DataTable
.
Also, you can use the Rows
property of the DataTable to get an enumerable of all rows instead of doing the AsEnumerable
call.
Edit:
By inspecting the decompiled sources for the DataTable I found that the Rows property of the DataTable returns a DataRowCollection object. The DataRowCollection stores the rows in a binary tree based structure that allows you to fetch the items based on its array index. So it will return the rows in the same order as they were added. As long as we expose an indexer that takes a numerical index this is implied.
In addition, the AsEnumerable
extension method, will turn the Rows into an EnumerableRowsCollection
that wraps the same types as an IEnumerable<DataRow>
.