84

I have a DataTable with multiple columns. I want to get a List<String> out of first column of DataTable. How can I do that?

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
shrishjain
  • 841
  • 1
  • 6
  • 3

7 Answers7

94

Try this:

static void Main(string[] args)
{
    var dt = new DataTable
    {
        Columns = { { "Lastname",typeof(string) }, { "Firstname",typeof(string) } }
    };
    dt.Rows.Add("Lennon", "John");
    dt.Rows.Add("McCartney", "Paul");
    dt.Rows.Add("Harrison", "George");
    dt.Rows.Add("Starr", "Ringo");

    List<string> s = dt.AsEnumerable().Select(x => x[0].ToString()).ToList();

    foreach(string e in s)
        Console.WriteLine(e);

    Console.ReadLine();
}
Michael Buen
  • 38,643
  • 9
  • 94
  • 118
62
var list = dataTable.Rows.OfType<DataRow>()
    .Select(dr => dr.Field<string>(columnName)).ToList();

[Edit: Add a reference to System.Data.DataSetExtensions to your project if this does not compile]

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
devdigital
  • 34,151
  • 9
  • 98
  • 120
  • 13
    AsEnumerable() works on a DataTable as well. var list = dataTable.AsEnumerable().Select(r => r.Field(columnName)).ToList(); – Wes Grant Oct 04 '11 at 19:40
  • If `DataTable.AsEnumerable()` does not compile, add a reference to System.Data.DataSetExtensions to your project. – Brk Nov 26 '13 at 12:57
  • 2
    What if you don't know the `columnName`? – chiapa Jul 19 '16 at 11:50
  • 1
    I would choose AsEnumerable() method instead of this. This is like holding the right ear with left hand. – Cengiz Araz Sep 14 '17 at 11:26
9

Here you go.

           DataTable defaultDataTable = defaultDataSet.Tables[0];
           var list = (from x in defaultDataTable.AsEnumerable()
                    where x.Field<string>("column1") == something
                    select x.Field<string>("column2")).ToList();

If you need the first column

           var list = (from x in defaultDataTable.AsEnumerable()
                    where x.Field<string>(1) == something
                    select x.Field<string>(1)).ToList();
Mangesh
  • 3,987
  • 2
  • 31
  • 52
7

Is this what you need?

DataTable myDataTable = new DataTable();
List<int> myList = new List<int>();
foreach (DataRow row in myDataTable.Rows)
{
    myList.Add((int)row[0]);
}
mario.tco
  • 674
  • 1
  • 7
  • 19
5

I do just like below, after you set your column AsEnumarable you can sort, order or how you want.

 _dataTable.AsEnumerable().Select(p => p.Field<string>("ColumnName")).ToList();
nzrytmn
  • 6,193
  • 1
  • 41
  • 38
4

I make a sample for you , and I hope this is helpful...

    static void Main(string[] args)
    {
        var cols = new string[] { "col1", "col2", "col3", "col4", "col5" };

        DataTable table = new DataTable();
        foreach (var col in cols)
            table.Columns.Add(col);

        table.Rows.Add(new object[] { "1", "2", "3", "4", "5" });
        table.Rows.Add(new object[] { "1", "2", "3", "4", "5" });
        table.Rows.Add(new object[] { "1", "2", "3", "4", "5" });
        table.Rows.Add(new object[] { "1", "2", "3", "4", "5" });
        table.Rows.Add(new object[] { "1", "2", "3", "4", "5" });

        foreach (var col in cols)
        {
            var results = from p in table.AsEnumerable()
                          select p[col];

            Console.WriteLine("*************************");
            foreach (var result in results)
            {
                Console.WriteLine(result);
            }
        }


        Console.ReadLine();
    }
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
1

1.Very Simple Code to iterate datatable and get columns in list.

2.code ==>>>

 foreach (DataColumn dataColumn in dataTable.Columns)
 {
    var list = dataTable.Rows.OfType<DataRow>()
     .Select(dataRow => dataRow.Field<string> 
       (dataColumn.ToString())).ToList();
 }
DennisLi
  • 3,915
  • 6
  • 30
  • 66
  • 1
    might want to mention that this 'simple' code requires .Net 4.5 or higher AND a nuget package which you have to install via "Install-Package System.Data.DataSetExtensions -Version 4.5.0" – PastExpiry.com Apr 17 '20 at 15:26