I have
DataTable dt;
DataRow[] drArray = dt.Select().ToArray();
My requirement is i want to convert drArray
as List<string[]>
or
Converting Datatable to List<string[]>
in a fastest way.
The string version of a DataRow is "System.Data.DataRow", because the system has no idea what the DataRow may contain, what part (s) of it you might want to display, or how you want them formatted.
You cannot convert a row into a string like that, a Row is a collection of cells not a single entity. You must take the values of all the cells you want in the string, and concatenate them (use a StringBuilder).
This article shows 3 ways to convert a DataTable to a List in C#. This article explains various ways to convert a DataTable to a List in C#. There are the following 3 ways to convert a DataTable to a List. Using a Loop. Using LINQ. Using a Generic Method. For this example I am creating a simple Student class like:
It makes no sense to convert a DataTable to a String array. What output would you expect? – Sam Axe Jul 11 2015 at 8:12 ToList() is returning List<System.Data.DataRow> instead i want to convert this as List<string[])
I think this will get you what you want:
List<string[]> results =
dt.Select()
.Select(dr =>
dr.ItemArray
.Select(x => x.ToString())
.ToArray())
.ToList();
This only works if the items stored in dr.ItemArray
have overriden .ToString()
in a meaningful way. Luckily the primitive types do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With