user1463065
user1463065

Reputation: 595

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

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.

Upvotes: 5

Views: 30040

Answers (1)

Enigmativity
Enigmativity

Reputation: 117037

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.

Upvotes: 10

Related Questions