0

I have array of string from the DataTable, so i'm use this code to get list :

ListTid = string.Join(",", DtTable.AsEnumerable().Select(r => r["NameColumn"].ToString()));

then i have result like this :

LIS1, LIST2, LIST3

How if i want result like this

'List1','List2','List3'
7
  • So you want 's in addition to ,s between each element? What have you tried? Commented Jan 26, 2015 at 2:59
  • 3
    Please tell me you're not going to use this in an sql query. It smells like an sql injection vulnerability waiting to happen. Commented Jan 26, 2015 at 3:07
  • @JoelCoehoorn I'm hopefully optimistic it's for a CSV write function. Commented Jan 26, 2015 at 3:28
  • i have list data on array =1,2,3,4 and want send to query parameter, like this exec storedprocedure @parameter='1','2','3','4' Commented Jan 26, 2015 at 4:28
  • 1
    Yeah, that's probably vulnerable to sql injection. Look into a table-valued parameter. Commented Jan 26, 2015 at 5:13

2 Answers 2

3

You can continue to use String.Join, just add the extra quotes at the end, like so:

ListTid = '\'' + String.Join("', '", DtTable.AsEnumerable().Select( r => r["NameColumn"].ToString() ) ) + '\'';
Sign up to request clarification or add additional context in comments.

Comments

2

Add the quotes in your select code, like this:

ListTid = string.Join(",", DtTable.AsEnumerable().Select(r => string.Format("'{0}'",Convert.ToString(r["NameColumn"])));

Note that I have changed your code to use Convert.ToString since it can safely handle nulls, whereas ToString will throw an exception.

2 Comments

Careful, this approach results in extra strong concatenation operations which means extra memory usage.
thist result '''TED00000'''',''''TED00001'''',''''TED00002'''',''''TED00003''''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.