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();
Share:
242,258
Lalit
Author by

Lalit

Software Developer on microsoft technologies, Nothing more, Nothing Less...

Updated on June 15, 2021

Comments

  • Lalit
    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
    Lalit about 13 years
    i 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
    Daniel Hilgarth about 13 years
    Just like this: string[] columnNames = dt.Columns.Cast<DataColumn>().Where(x => x.ColumnName != "ABC").Select(x => x.ColumnName).ToArray();
  • Admin
    Admin almost 13 years
    On my end, this throws two Exceptions: cannot convert from DataColumnCollection to EnumerableRowCollection and DataColumnCollection does not contain a definition for Cast.
  • Sem Vanmeenen
    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
    Admin almost 13 years
    Doh! You are quite correct; funny, that using statement is automatically added so often, it never occurred to me to check for it.
  • Tizz
    Tizz about 11 years
    I still get 'Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Data.DataColumn'. What is going on?
  • Daniel Hilgarth
    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
    Rod almost 7 years
    if 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).ToArra‌​y()) { ... }
  • FLICKER
    FLICKER over 5 years
    This 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
    Daniel Hilgarth over 5 years
    @FLICKER: Some thinking is still required as a developer. You have a brain. Use it.
  • Daniel Hilgarth
    Daniel Hilgarth over 5 years
    Ah well. Just look at the votes. Obviously, the problem is with you, not with the code. Go troll somewhere else