Convert list to string with single quotes

asp.net, c#, list

Solution

Try this:

string SalesManCode = string.Join(",", SelectedSalesmen
                                            .Select(x=>string.Format("'{0}'",x)));

it will wrap all your elements with `'` and then join them using `,` as separator

Problem

I am trying to create a string from List This is my code ``` List<string> SelectedSalesmen = new List<string>(); ``` and I am adding selected salesmen from listBox like this ``` foreach (ListItem lst in lstBoxSalesmen.Items) { if (lst.Selected) { SelectedSalesmen.Add(lst.Value); } } ``` finally I am storing that value to a string like this ``` string SalesManCode = string.Join(",", SelectedSalesmen.ToArray()); ``` But I am getting like this ``` SLM001,SLM002,SLM003 ``` but I need Output like this ``` 'SLM001','SLM002','SLM003' ```

Original source

Related problems