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;
Kay Lee
  • 922
  • 1
  • 12
  • 40
Lalit
  • 4,897
  • 7
  • 32
  • 36

4 Answers4

278

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.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 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 Feb 17 '11 at 08:14
  • Just like this: `string[] columnNames = dt.Columns.Cast().Where(x => x.ColumnName != "ABC").Select(x => x.ColumnName).ToArray();` – Daniel Hilgarth Feb 17 '11 at 08:18
  • I still get 'Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Data.DataColumn'. What is going on? – Tizz May 16 '13 at 20:17
  • 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. – Daniel Hilgarth May 17 '13 at 08:08
  • 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().Select(x=>x.ColumnName).ToArray()) { ... }` – Rod Jul 21 '17 at 17:35
  • 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 – FLICKER Nov 21 '18 at 18:43
  • 3
    @FLICKER: Some thinking is still required as a developer. You have a brain. Use it. – Daniel Hilgarth Nov 22 '18 at 06:20
  • 1
    Ah well. Just look at the votes. Obviously, the problem is with you, not with the code. Go troll somewhere else – Daniel Hilgarth Nov 22 '18 at 06:45
19

Use

var arrayNames = (from DataColumn x 
                  in dt.Columns.Cast<DataColumn>()
                  select x.ColumnName).ToArray();
Sem Vanmeenen
  • 2,111
  • 11
  • 13
  • 1
    On my end, this throws two Exceptions: cannot convert from DataColumnCollection to EnumerableRowCollection and DataColumnCollection does not contain a definition for Cast. –  Jul 27 '11 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. – Sem Vanmeenen Jul 28 '11 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. –  Aug 03 '11 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();
Deilan
  • 4,740
  • 3
  • 39
  • 52
0
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]));
    }
}
vibs2006
  • 6,028
  • 3
  • 40
  • 40
user3233312
  • 166
  • 1
  • 1
  • 6