I have a list of strings that I want to encapsulate in quotes and separate with comma and output as single string.
Right now I got only the comma part like so string.Join(",", list);. This generates output like so Id, Name, Price. However I want to add quotes to every item so it will output it like so "Id", "Name", "Price".
Right now I'm looping through every item in list copying it's value and then adding the quotes but I suspect it is possible to do using LINQ.
Try this
String.Join(",", myList.Select(x => String.Format("\"{0}\"", x));
I suspect it is possible to do using LINQ
You are correct:
var result = string.Join(",", list.Select(s => '"' + s + '"') );
Right now I'm looping through every item in list copying it's value and then adding the quotes
Well, that's what Linq is doing behind-the-scenes; it's just wrapping it in a .Select extension method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With