16

Is there any way to convert a list of strings to a comma-separated string?

String[] data = new String[] { "test", "abc", "123" }

Convert into:

'test', 'abc', '123'

Possible solutions:

  1. Surround every string with '' and then use String.join on the list.
  2. Foreach each string in the list and do the concatenation of '' and ',' and in the end remove last ','

Is there any simple Linq (one line expression) to do both?

3
  • 1
    Use var x = string.Join(", ", yourArray); Commented Nov 2, 2015 at 5:57
  • @slugster, I hope I mentioned it in my approaches Commented Nov 2, 2015 at 5:59
  • Side note: make sure you are not trying to build JSON... may endup with square reinvented wheel. Commented Nov 2, 2015 at 6:07

5 Answers 5

31

Is there any simple Linq (one line expression) to do both.

string.Join(",", data.Select(item => "'" + item + "'"))

Basics of Linq: Transforms are Select statements. Filters are Where statements.

That said, there are a lot of string manipulation tools available that aren't Linq, and they're more likely to be optimized for strings, so I'd always look to them before looking to Linq.

Sign up to request clarification or add additional context in comments.

5 Comments

I linked to another question that is now deleted - stackoverflow.com/a/33471600/232593 - I think their code probably has better performance than mine, even though it doesn't use Linq. return "'" + string.Join("','", data) + "'";
@AsadSaeeduddin - if you want to undelete your answer, I know I'd personally upvote it, even though there isn't any Linq to be found.
I also think Using linq everywhere is not good. When you look at the linq methods from inside you will see that you make a mess inside your program just to do a simple count!
@M.kazemAkhgary There is something to be said for readability and premature optimization, but I agree with you that being able to understand and make these tradeoffs is good. I call them out because I like people to be aware there is a tradeoff to make.
I was going to upvote AsadSaeeduddin's answer when he deleted it. I think his answer is good because of the simplicity. Maybe he deleted it because OP wants a solution using LINQ.
3

You can use aggregate linq

Array.Skip(1).Aggregate(Array[0],(a,b) => string.Format("{0},'{1}'",a,b));

1 Comment

While smart looking, don't ever use that for real code with more than couple items due to O(n^2) performance...
2
String[] data = new String[]{"test","abc","123"};
var result = string.Join(",", data.Select(o => string.Concat("'",o,"'"));

2 Comments

Style preference - why would you use Concat over + which generates identical code?
@AlexeiLevenkov for me, it's more easy to read ,-separated arguments, ecpecially when it contains other operation like + and condition ? true : false. for example compare "a" + ((a>1) ? "yes: " + variable : "no one") + "b" and string.Concat("a", (a>1) ? "yes: " + variable : "no", "b")
0

You could also use the Aggregate method: Example:

List<string> fruit = new List<string> {"Apple", "Orange", "Pear", "Tomato", "Banana"};
var fruitSentence = fruit.Aggregate((current, next) => $"{current},{next}");

Comments

0

NOTE: if you're starting with IEnumerable or similar you have to call .ToArray() at the end of the LINQ statement like this:

input parameter: IEnumerable<string> feederIdList    

var feederListString = String.Join(",", feederIdList.Select(feeder => "\"" + feeder + "\"").ToArray());

In my case I needed each string to have double quotes around it for passing into an Oracle stored procedure later.

Comments

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.