hi, experts, I dont know where to post, therefore, I posted here.
I have a datatable like below one. How to convert to this datatable to List<string[]> ?? List of string array, NOT list of array?
I need to do something to send to page via JSON.
Or is it possible to convert the datatable directly to JSON? but datatable to List<string[]> is preferred. Thank you very much!
public static DataTable GetDefaultDataTable()
{
DataTable dt = new DataTable("Default");
DataColumn dc = new DataColumn("UserId", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("UserName", typeof(int));
dt.Columns.Add(dc);
dc = new DataColumn("FirstName", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("MiddleName", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("LastName", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Email", typeof(string));
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["UserId"] = "1";
dr["FirstName"] = "Peter";
dr["MiddleName"] = "BC";
dr["LastName"] = "Tan";
dr["Email"] = "peter.bc.tan@gmail.com";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["UserId"] = "2";
dr["FirstName"] = "Jack";
dr["MiddleName"] = "KK";
dr["LastName"] = "Won";
dr["Email"] = "jack.kk.won@gmail.com";
dt.Rows.Add(dr);
return dt;
}
delaynomore
Member
9 Points
18 Posts
datatable to List of string array
Nov 26, 2012 12:25 PM|LINK
hi, experts, I dont know where to post, therefore, I posted here.
I have a datatable like below one. How to convert to this datatable to List<string[]> ?? List of string array, NOT list of array?
I need to do something to send to page via JSON.
Or is it possible to convert the datatable directly to JSON? but datatable to List<string[]> is preferred. Thank you very much!
public static DataTable GetDefaultDataTable() { DataTable dt = new DataTable("Default"); DataColumn dc = new DataColumn("UserId", typeof(string)); dt.Columns.Add(dc); dc = new DataColumn("UserName", typeof(int)); dt.Columns.Add(dc); dc = new DataColumn("FirstName", typeof(string)); dt.Columns.Add(dc); dc = new DataColumn("MiddleName", typeof(string)); dt.Columns.Add(dc); dc = new DataColumn("LastName", typeof(string)); dt.Columns.Add(dc); dc = new DataColumn("Email", typeof(string)); dt.Columns.Add(dc); DataRow dr = dt.NewRow(); dr["UserId"] = "1"; dr["FirstName"] = "Peter"; dr["MiddleName"] = "BC"; dr["LastName"] = "Tan"; dr["Email"] = "peter.bc.tan@gmail.com"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["UserId"] = "2"; dr["FirstName"] = "Jack"; dr["MiddleName"] = "KK"; dr["LastName"] = "Won"; dr["Email"] = "jack.kk.won@gmail.com"; dt.Rows.Add(dr); return dt; }sargamlucy
Member
559 Points
164 Posts
Re: datatable to List of string array
Nov 26, 2012 12:47 PM|LINK
Create a string array with a capacity = no of columns in your datatable. Create a list of string array type.
Loop through each row in your datatable.
Add each column as one item to that string array. At the end of each row, add the string array to the list. Then clear the values from the array.
Repeat the same for all rows.
Regards,
Snigdha
Please Mark as Answer if my reply helped you.
RichardD
Contributor
3950 Points
549 Posts
Re: datatable to List of string array
Nov 26, 2012 04:11 PM|LINK
This is fairly easy with LINQ:
static List<string[]> ConvertTable(DataTable table) { return table.Rows.Cast<DataRow>() .Select(row => table.Columns.Cast<DataColumn>() .Select(col => Convert.ToString(row[col])) .ToArray()) .ToList(); }aarsh
Participant
1543 Points
427 Posts
Re: datatable to List of string array
Nov 26, 2012 09:14 PM|LINK
may be this can help ? http://sdrv.ms/TXfAYE ... just ignore the LINQ part ...