NadimAJ
NadimAJ

Reputation: 137

Convert ArrayList to List<string[]>

I have a whole bunch of functions which return the data type ArrayList. Each list item contains a string value which has multiple values all delimited by a "\t" and up until now I've been appending a text box with the data. I've done this so I can take the output and copy/paste straight into Excel as it was a simple requirement up till now..

I would like to convert the ArrayList (which contains multiple tab delimited strings) to a List of string arrays, i.e.

List<string[]> 

This is so I can then attempt to fill a DataGridView control.

Anybody point me in the right direction? Thanks

Upvotes: 0

Views: 1766

Answers (2)

dcastro
dcastro

Reputation: 68660

Like this?

List<string> list = arrayList.Cast<string>()
         .SelectMany(s => s.Split('\t'))
         .ToList();

This casts every object in the arrayList to a string, splits each string in a set of strings, and flattens the "set of sets" into one list of strings.

Or this, if you don't want to flatten the set:

List<string[]> list = arrayList.Cast<string>()
     .Select(s => s.Split('\t'))
     .ToList();

Upvotes: 3

Cyril Gandon
Cyril Gandon

Reputation: 17048

First, cast your arrayList to an enumerable of string.
Then for each string, split it into another enumerable of string, with \t separator, and flatten it to an array.

List<string[]> listOfArray = myArrayList.Cast<string>()
                                        .Select(s => s.Split('\t').ToArray())
                                        .ToList();

Upvotes: 1

Related Questions