139

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;
0

4 Answers 4

283

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.

8
  • 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.
    – Lalit
    Commented Feb 17, 2011 at 8:14
  • Just like this: string[] columnNames = dt.Columns.Cast<DataColumn>().Where(x => x.ColumnName != "ABC").Select(x => x.ColumnName).ToArray(); Commented Feb 17, 2011 at 8:18
  • 1
    @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. Commented May 17, 2013 at 8:08
  • 3
    @FLICKER: Some thinking is still required as a developer. You have a brain. Use it. Commented Nov 22, 2018 at 6:20
  • 1
    Ah well. Just look at the votes. Obviously, the problem is with you, not with the code. Go troll somewhere else Commented Nov 22, 2018 at 6:45
19

Use

var arrayNames = (from DataColumn x 
                  in dt.Columns.Cast<DataColumn>()
                  select x.ColumnName).ToArray();
3
  • 1
    On my end, this throws two Exceptions: cannot convert from DataColumnCollection to EnumerableRowCollection and DataColumnCollection does not contain a definition for Cast.
    – user565869
    Commented Jul 27, 2011 at 15:48
  • 4
    @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. Commented Jul 28, 2011 at 13:03
  • Doh! You are quite correct; funny, that using statement is automatically added so often, it never occurred to me to check for it.
    – user565869
    Commented Aug 3, 2011 at 15:01
6

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();
-1
List<String> lsColumns = new List<string>();

if(dt.Rows.Count>0)
{
    var count = dt.Rows[0].Table.Columns.Count;

    for (int i = 0; i < count;i++ )
    {
        lsColumns.Add(Convert.ToString(dt.Rows[0][i]));
    }
}
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.