Get all column names of a DataTable into string array using (LINQ/Predicate)
242,258
Solution 1
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.
Solution 2
Use
var arrayNames = (from DataColumn x
in dt.Columns.Cast<DataColumn>()
select x.ColumnName).ToArray();
Solution 3
I'd suggest using such extension method:
public static class DataColumnCollectionExtensions
{
public static IEnumerable<DataColumn> AsEnumerable(this DataColumnCollection source)
{
return source.Cast<DataColumn>();
}
}
And therefore:
string[] columnNames = dataTable.Columns.AsEnumerable().Select(column => column.Name).ToArray();
You may also implement one more extension method for DataTable
class to reduce code:
public static class DataTableExtensions
{
public static IEnumerable<DataColumn> GetColumns(this DataTable source)
{
return source.Columns.AsEnumerable();
}
}
And use it as follows:
string[] columnNames = dataTable.GetColumns().Select(column => column.Name).ToArray();
Author by
Lalit
Software Developer on microsoft technologies, Nothing more, Nothing Less...
Updated on June 15, 2021Comments
-
Lalit almost 3 years
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;
-
Lalit about 13 yearsi am a newbie in linq/lamda ex. This looks good. one more question, how can i place condation(where dc.ColumnName != "ABC") in 1 lamda expression. in linq i can use where.
-
Daniel Hilgarth about 13 yearsJust like this:
string[] columnNames = dt.Columns.Cast<DataColumn>().Where(x => x.ColumnName != "ABC").Select(x => x.ColumnName).ToArray();
-
Admin almost 13 yearsOn my end, this throws two Exceptions: cannot convert from DataColumnCollection to EnumerableRowCollection and DataColumnCollection does not contain a definition for Cast.
-
Sem Vanmeenen almost 13 years@Jon I think you forogot to add 'using System.Linq;' to your usings. I just tested my code and I get the exceptions you mention when 'using System.Linq;' isn't there.
-
Admin almost 13 yearsDoh! You are quite correct; funny, that
using
statement is automatically added so often, it never occurred to me to check for it. -
Tizz about 11 yearsI still get 'Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Data.DataColumn'. What is going on?
-
Daniel Hilgarth about 11 years@Tizz: Please post it as a new question and include details of your code. But essentially: A DataGrid is not the same as a DataTable.
-
Rod almost 7 yearsif you put the former snippet in a for each loop, will it do all that casting for each item?
foreach(string item in dt.Columns.Cast<DataColumn>().Select(x=>x.ColumnName).ToArray()) { ... }
-
FLICKER over 5 yearsThis has syntax error. I guess because you have used a specific assembly. Please add using part in your answer when you use special things
-
Daniel Hilgarth over 5 years@FLICKER: Some thinking is still required as a developer. You have a brain. Use it.
-
Daniel Hilgarth over 5 yearsAh well. Just look at the votes. Obviously, the problem is with you, not with the code. Go troll somewhere else