How to convert a single data column to csv?

How to convert single datatable column to csv? I have a datatable that have two columns (Id, name), how can I convert an Id column to a csv string?

+4
source share
3 answers

What is the expected result? A line where each identifier is separated by a comma or a line, where is the identifier separated by newline characters?

However, you can use String.Join+ LINQ:

string result = String.Join(
    Environment.NewLine, // replace with "," if desired
    table.AsEnumerable().Select(row => row.Field<int>("ID")));
+6
source

If you want to write to a file, you can do this:

var dt=new DataTable();
    dt.Columns.Add("Id",typeof(int));
    dt.Columns.Add("name",typeof(string));

    dt.Rows.Add(1,"test");

    File.WriteAllLines("C:\\test\\test.csv",
        dt.AsEnumerable()
            .Select(d =>
                string.Format("{0},{1}",d.Field<int>("Id"),d.Field<string>("name")))
        );
+2
source

, Toad Mysql Or Oracle.

PHP, , .

PHP csv

0

All Articles