简体   繁体   中英

Creating a string from a generic list

I'm trying to create a string from the values in a list;what i am trying to achieve is the sql syntax wich is used in an update query:

UPDATE TABLE SET COLUMN1 =X WHERE COLUMN2 IN ('A','B','C')

(A,B,C are items in my list.)How can i achieve this? tried:

            string commaSeparatedList = _list.Aggregate((a, x) => a + ", " + x);

but it creates me the list without the apostrophes...

您也可以改用String.Join方法

string commaSeparatedList = string.Join(",", _list.Select(s => "'" + s + "'"));

If your code gives you exactly what you want except for the apostrophes, just stick a

.Select(s => "'" + s + "'")

between _list .and .Aggregate...

string commaSeparatedList = "'"+_list.Aggregate((a, x) =>  a + "','" + x )+"'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM