Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I need to get a comma sperated list from a datatable column. This part i am able to complete. But my requirement is to enclose single quotes between each item.
For example 'ÁBC','DEF','GHI'
I need to get output like this. Can any one please modify my code to get the desired output. Currently i am returing array of string but i am happy if i can get as a string

C#
public static string[] GetCommaSperatedList(DataTable dt)
{
  string[] allitems = dt.AsEnumerable()
                         .SelectMany(r => r.Field<string>("IN_KEY_TMS").Split(','))
                         .ToArray();
  return allitems;
}


Thanks in advance
Posted

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.
C#
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[^]
 
Share this answer
 
v2
Comments
jinesh sam 14-May-15 6:54am    
@CHill60 Is there any possibilty to incoprate this code to my linq query.
so that i can avoid two lines of code.
jinesh sam 14-May-15 6:55am    
I am asking for a single query which will do two things.
CHill60 14-May-15 7:07am    
Well strictly speaking you didn't ask for that ("but i am happy if i can get as a string")
- however I have updated my solution with this latest requirement
jinesh sam 14-May-15 7:34am    
Thanks CHill :)
Maciej Los 14-May-15 7:49am    
+5!
Try this:

C#
public static string[] GetCommaSperatedList(DataTable dt)
{
  return String.Join(",", dt.AsEnumerable()
                         .SelectMany(r => r.Field<string>("IN_KEY_TMS").Split(',')).ToArray());
}
 
Share this answer
 
v2
Comments
jinesh sam 14-May-15 6:48am    
@Maciej Here you are not enclosing single qutoes. My requirement was that
CHill60 14-May-15 6:49am    
Apologies for the overlap ... I really must learn to refresh the page before adding a solution :-O
Maciej Los 14-May-15 7:49am    
No problem, Caroline ;)
jinesh sam 14-May-15 6:58am    
No worries

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900