I know we can easily do this by a simple loop, but I want to persue this LINQ/Predicate?
string[] columnNames = dt.Columns.?
or
string[] columnNames = from DataColumn dc in dt.Columns select dc.name;
Try this (LINQ method syntax):
string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();
or in LINQ Query syntax:
string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>()
select dc.ColumnName).ToArray();
Cast
is required, because Columns is of type DataColumnCollection which is a IEnumerable
, not IEnumerable<DataColumn>
. The other parts should be obvious.