Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert DataRow[] to List<string[]>

Tags:

c#

asp.net

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.

like image 290
user1463065 Avatar asked Jul 11 '15 07:07

user1463065


People also ask

What is the string version of a datarow?

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.

How do I convert a row to a string in Excel?

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).

How to convert a DataTable to a list in C #?

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:

Can you convert a DataTable to a string array?

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[])


1 Answers

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.

like image 147
Enigmativity Avatar answered Oct 17 '22 12:10

Enigmativity