0

I have string array ["x3;s4;r4", "x2;s6;r7", ...] that contains sort of 'multidimensional' data. I do following LINQ:

var fieldsArrays = data.Select(s => s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));

This query returns true multidimensional array [["x3", "s4", "r4"], [...]] I need to select all values into a one dimensional array: ["x3", "s4", "r4", "x2", "s6"]

What is the LINQ magic for this please?

Thanks!

Luke1988
  • 1,850
  • 2
  • 24
  • 42

1 Answers1

6

You can use SelectMany for this:

var fieldsArrays = data.SelectMany(s => s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));

You'll want to add a .ToArray(); onto the end if you do actually want it back into an array (rather than acting as a view over data).

Try it online

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    So simple?! Works great. Give this man a beer! Thanks a lot (yeah and I feel like a 6 year old kid now...) – Luke1988 Apr 28 '20 at 08:04