Convert Datatable column to string Array in C# and VB.Net

amar
 
on Oct 20, 2021 04:52 AM
426 Views

How to Convert Datatable values to string Array simple code without loop.

Only one column

Name
ABC
xyz
saq
ede

Output should

string [] strval={"ABC"}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 20, 2021 05:16 AM

Check the below code.

C#

DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Rows.Add("John Hammond");
dt.Rows.Add("Mudasssar Khan");
dt.Rows.Add("Suzanne Mathews");
dt.Rows.Add("Robert Schidner");
string[] names = dt.AsEnumerable().Select(r => r["Name"].ToString()).ToArray();

VB.Net

Dim dt As DataTable = New DataTable()
dt.Columns.Add("Name")
dt.Rows.Add("John Hammond")
dt.Rows.Add("Mudasssar Khan")
dt.Rows.Add("Suzanne Mathews")
dt.Rows.Add("Robert Schidner")
Dim names As String() = dt.AsEnumerable().Select(Function(r) r("Name").ToString()).ToArray()