To return the contents surrounded by single quotes use
ConvertAll
e.g.
Array.ConvertAll(allitems, z => "'" + z + "'");
To combine them all into a single string (once surrounded by quotes) then use
Join
return String.Join(",", Array.ConvertAll(allitems, z => "'" + z + "'"));
[EDIT in response to OP comment]
Just combine the two lines...cut&paste the bit that derives
allitems
over the use of "allitems" in the code I supplied i.e.
public static string GetCommaSperatedList(DataTable dt)
{
return String.Join(",",Array.ConvertAll(dt.AsEnumerable()
.SelectMany(r => r.Field<string>("IN_KEY_TMS").Split(',')).ToArray(), z => "'" + z + "'"));
}
Note I've changed the return type of the function from string[] to string.
Additionally NOTE!
If you are planning to use this string in a SQL query and if the datatable has been influenced/created from user input then you may be exposing your code to a risk of SQL Injection. If this is the case then also consider the Solution from Richard Deeming to this post
Fetching specific values from a database using user input array. It works for single items but not for a list of itemids[
^]