DataColumnCollection implements IEnumerable , and each row returned is a DataColumn , but does not implement IEnumerable<DataColumn> . Since it does not implement an interface, you cannot pass it to an interface. Since the class is sealed, the compiler knows that this value cannot implement the interface, so you cannot even give it at compile time.
Use the LINQ Cast insted method:
table.Columns.Cast<DataColumn>()
This is an efficient adapter method — every element in a column collection will be lazily discarded by the DataColumn when you extract it from the result.
The reason for compiling foreach is because the compiler adds an explicit cast for you. For example, this will compile:
foreach (string x in table.Columns) { }
... but it will throw an exception at runtime.
Jon skeet
source share