Search code examples
c#datatable

How to save datatable first column in array C#


I have this kind of datatable:

Name | CategorieID | FullCategorie_ID
----  -------------  ----------------
 A        1             12  
 B        1             13
 C        5             14
 D        3             15
 E        6             16

I want to save the values of column Name in an array, I am using this to get the values of a row. E.g. To get the values of the first row I can use the following code.

var stringArr =datatable1.Rows[0].ItemArray.Select(x => x.ToString()).ToArray();

But I don't know how to get all the values of the only first column.


Solution

  • I suggest to use LINQ to DataSet for querying DataTable:

    datatable1.AsEnumerable().Select(r => r.Field<string>("Name")).ToArray();