How to convert datatable to string value

I dont know if you still need a solution. However, i have learned that the more versatile way to log or write all to a string is using string.join and LINQ.

Basic case and works always:

       string.join(Environment.NewLine, yourDataTable.Rows.Cast(of DataRow).Select(function(row) string.join(",",row.ItemArray))

Just copy and paste and change yourDataTable to your variable.

In case you want to understand and goo deeply you can do a lot of changes some of them:

If you add take()/skip() before .Select you can take or skip a number of items of the table for example skip(1) would skip the header in case you have read it as a normal row.

If you want to get some special data (just one item of the row/ or do some changes before going to a string) you just have to change the content of the select for example:

.Select(function(row) row(0).tostring) will show only the first column data
.Select(function(row) string.join(“,”,row.ItemArray.Take(3)) will show only the first 3 rows

And much more :slight_smile:

I wish i have help and remember to mark a solution if you had one.

17 Likes