141

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

286

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
Just like this: string[] columnNames = dt.Columns.Cast<DataColumn>().Where(x => x.ColumnName != "ABC").Select(x => x.ColumnName).ToArray();
@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.
@FLICKER: Some thinking is still required as a developer. You have a brain. Use it.
Ah well. Just look at the votes. Obviously, the problem is with you, not with the code. Go troll somewhere else
|
19

Use

var arrayNames = (from DataColumn x 
                  in dt.Columns.Cast<DataColumn>()
                  select x.ColumnName).ToArray();

3 Comments

On my end, this throws two Exceptions: cannot convert from DataColumnCollection to EnumerableRowCollection and DataColumnCollection does not contain a definition for Cast.
@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.
Doh! You are quite correct; funny, that using statement is automatically added so often, it never occurred to me to check for it.
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();

Comments

-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 Comment

This makes a perfect solution worse and unintelligible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.