This code works:
foreach(DataColumn column in table.Columns)
{
// do whatever
}
But this code doesn't:
(IEnumerable<DataColumn>)(table.Columns)
The .Columns
property returns a DataColumnCollection which is an InternalDataCollectionBase, which implements IEnumerable, so it should work.
The error I get is
Cannot convert type 'System.Data.DataColumnCollection' to 'System.Collections.Generic.IEnumerable'
DataColumnCollection
implements IEnumerable
, and each returned row is a DataColumn
, but it doesn't implement IEnumerable<DataColumn>
. As it doesn't implement the interface, you can't cast to the interface. As the class is sealed, the compiler knows that the value can't possibly implement the interface, so you can't even cast to it at compile-time.
Use the LINQ Cast
method insted:
table.Columns.Cast<DataColumn>()
That's effectively an adapter method - each element in the column collection will be lazily cast to DataColumn
as you fetch it from the result.
The reason the foreach
compiles is that the compiler adds an explicit cast for you. For instance, this will compile:
foreach (string x in table.Columns)
{
}
... but it'll throw an exception at execution time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With