tags:

views:

136

answers:

2
foreach (DataRow row in myDataTable.Select())

foreach (DataRow row in myDataTable.AsEnumerable())

foreach (DataRow row in myDataTable.Rows)

Any differences?

+3  A: 

Rows isn't strongly typed - so there will be a cast on each iteration, and you can't use LINQ to objects on it easily. (I believe AsEnumerable() will have to cast on each iteration internally as well, but at least you can use it for other LINQ methods easily.)

Select needs to build an array, so there's obviously a performance penalty there.

Personally I'd use AsEnumerable() unless you wanted to modify the table within the loop, in which case the fact that Select builds an array up-front may actually be an advantage.

Jon Skeet
A: 

Hey,

Use AsEnumerable() if you are trying to query the datatable using LINQ, otherwise, you might as well use the looping structure...

Brian