0

I need to generate a string that has a comma delimited list, but no comma after the last element.

var x = new List<string>() { "a", "b", "c" };

should yield:

a,b,c

Yes, a very simple thing to do using "normal" techniques, but I hope with linq there is a more elegant way.

var cols =context.Database.SqlQuery<String>("select Column_Name from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = {0};", prefix + table);
2
  • 2
    Use String.Join to concatenate list items with coma String.Join(",", x) Commented Jan 27, 2014 at 17:39
  • not sure what the last line is but you need to be careful about where prefix and table come from. Commented Jan 27, 2014 at 17:44

3 Answers 3

2

No need Linq, just use String.Join

String.Join(",", new List<string>() { "a", "b", "c" });
Sign up to request clarification or add additional context in comments.

Comments

0

String class provide Join method to join string array with delimiter.

Code:

var x = new List<string>() { "a", "b", "c" };
String.Join(",",x.ToArray());

documentation: http://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx

Comments

0

Although I strongly recommend you use the answer of Siva Charan, just for information here's an implementation in LinQ using Enumerable.Aggregate Method (IEnumerable, Func):

var result = x.Aggregate((c, n) => c + "," + n);

Comments

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.