23 November 2011

LINQ - List DataTable columns

How can you get a List of DataTable columns using LINQ?
The dataTable.Columns property cannot be queried with LINQ because it's a DataColumnCollection. Such a collection implements IEnumerable, but it's not a IEnumerable<DataColumn>, and therefore must be cast as such:
List<string> columnNames = (from c in dataTable.Columns.Cast<datacolumn>()
                            select c.ColumnName).ToList();

No comments:

Post a Comment