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 which 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?
I tried:
string commaSeparatedList = _list.Aggregate((a, x) => a + ", " + x);
but it creates the list without the apostrophes.
You can also use String.Join Method instead:
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...