1

When I add rows to a Datatable, and later iterate that Datatable. Will I get the rows in the same order then they were inserted? First in, first out? Or can I not rely on that order?

 row = datatable.NewRow();
 row("id") = 1;       
 table.Rows.Add(row);
 row = datatable.NewRow();
 row("id") = 2;       
 table.Rows.Add(row);


foreach(Row row in datatable.AsEnumerable())
{
   // FIFO here? always get row with id 1 as first row and row with id 2 as second row?
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
DanielG
  • 1,217
  • 1
  • 16
  • 37

2 Answers2

1

If you do this manually, YES of course.

MSDN - Adding Data to a DataTable

Note that values in the array are matched sequentially to the columns, based on the order in which they appear in the table.

  • SQL-Server is not related at all. A `DataTable` is an in-memory collection which does not even need to be filled from a database. Apart from that, where is it documented that you can rely on it? – Tim Schmelter Jan 20 '14 at 08:12
  • I changed it. with MSDN Link – Smartis has left SO again Jan 20 '14 at 08:16
  • @Smartis: Your link doesn't help. The section you've mentioned targets the order of columns if you use `table.Rows.Add(array)`. The order in the array is the same of the ordinal-index of the column in the table. OP wants to know if it's guaranteed that the insertion order is the same as the output-order. I haven't found something at [`DataRowCollection.GetEnumerator`](http://msdn.microsoft.com/en-us/library/ms135386(v=vs.90).aspx). So you cannot rely on it. – Tim Schmelter Jan 20 '14 at 08:26
  • yeah...Tim is right. But could it be that this is obvious and so there is no statement about it? – DanielG Jan 20 '14 at 08:55
  • @user1682946: i would say it's very unlikely that microsoft will change the implementation in future so that the order is non-deterministic since that would be a breaking change. However, apparently it is not guaranteed. – Tim Schmelter Jan 20 '14 at 10:45
1

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>.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
  • Here iI'm also missing a link to the documentation that addresses the order of rows in the `DataTable`. Otherwise that's just guessing. – Tim Schmelter Jan 20 '14 at 08:28
  • @TimSchmelter good point. As far as I know the documentation doesn't answer this question, but the decompiled sources do :-) – Rune Grimstad Jan 20 '14 at 09:51
  • Even if implementation details can change if they are not documented, where have you find it in the code? I have looked at `DataRowCollection.GetEnumerator` but that yields just a strange (internal class) `RBTree`. So it seems to be implemented as a tree which feeds my doubts. – Tim Schmelter Jan 20 '14 at 10:03
  • @TimSchmelter Since we expose an indexer that allows you to access the rows by numerical index the ordering is implied. Otherwise the behaviour of the indexer would be completely random. – Rune Grimstad Jan 20 '14 at 10:06
  • RuneGrimstead: an indexer is just syntactic sugar. Even a `Dictionary` has an [indexer](http://msdn.microsoft.com/en-us/library/9tee9ht2(v=vs.110).aspx) to access a pair via key. But the order of a dictionary is non-deterministic, so it [is not guaranteed](http://stackoverflow.com/a/6384765/284240). – Tim Schmelter Jan 20 '14 at 10:11