C #: Iterating over a data table: rows, Select () or AsEnumerable ()

foreach (DataRow row in myDataTable.Select()) foreach (DataRow row in myDataTable.AsEnumerable()) foreach (DataRow row in myDataTable.Rows) 

Any differences?

+6
c # linq datatable
source share
2 answers

Rows not strongly typed - so a cast will be performed at each iteration, and you cannot easily use LINQ for objects. (I believe that AsEnumerable() should also use every iteration within itself, but at least you can easily use it for other LINQ methods.)

Select should build an array, so there obviously is a performance limitation there.

Personally, I would use AsEnumerable() if you don't want to modify the table in a loop, in which case the fact that Select building the array up can be an advantage.

+7
source share

Use AsEnumerable () if you are trying to query data using LINQ, otherwise you could use a loop structure ...

0
source share

All Articles