1

I have a list of strings:

List<string[]> myList

I want to convert it to a string separated by ",". I know how to convert List myList but not what i need.. I tried

String.Join(", ", myList.ToArray());

But i won't work for string[]

I tried to search the internet for solutions but could not found one... I know i can do it with foreach but im looking for one line solution, mostly to learn more advanced coding.

Thank you!

5
  • 5
    .SelectMany could be your friend. Commented Dec 2, 2015 at 12:40
  • 3
    One line doesn't necessarily mean advance coding Commented Dec 2, 2015 at 12:42
  • @Izzy I know, i mean like Daniel said, i wanted to find a function like SelectMany Commented Dec 2, 2015 at 12:49
  • @TimSchmelter ok sir. done :) Commented Dec 2, 2015 at 12:59
  • Looking forward the next super complicated question like "List of ints to string` Commented Dec 2, 2015 at 15:08

2 Answers 2

13

You can use Enumerable.SelectMany to flatten your List<string[]>:

string.Join(", ", myList.SelectMany(x => x));
Sign up to request clarification or add additional context in comments.

Comments

4

Use Join twice.

String.Join(", ", myList.Select(arr => "{" + String.Join(", ", arr) + "}"));

As mentioned by @TimSchmelter use this approach when you want to use different delimiters for each group. so you can join inner array by something like , and the outer list by / or any thing you prefer. also you can use braces to make it look better.

BTW if delimiters are same use the approach given by @YuvalItzchakov

1 Comment

Working but why do you use ", " and ",", so one time with space and one time without? OP hasn't mentioned that. This approach would be fine if that was a requirement, otherwise SelectMany is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.