Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list to string with single quotes

Tags:

c#

list

asp.net

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'
like image 639
Rakesh Avatar asked Jan 14 '14 11:01

Rakesh


1 Answers

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

like image 166
Kamil Budziewski Avatar answered Sep 24 '22 02:09

Kamil Budziewski