Click here to Skip to main content
15,894,405 members
Articles / General Programming / String
Alternative
Tip/Trick

Converting Array or List of data to String => made easy! (C#, MVC3, LINQ)

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
18 Jan 2012CPOL 15.7K   2
I would just use:string.Join(",", cities.Select(c=>c.Name))Since Version 4 of the Framework there is an overloaded versions of string.Join for IEnumerable too. It uses a StringBuilder internally and doesn't insert a seperator after the last element as well.I could't find the Join method...

I would just use:


C#
string.Join(",", cities.Select(c=>c.Name))

Since Version 4 of the Framework there is an overloaded versions of string.Join for IEnumerable too. It uses a StringBuilder internally and doesn't insert a seperator after the last element as well.


I could't find the Join method you mentioned in your last example with the search of my Reflector. Must be an extension to IList of string?! Where is it defined?

License

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


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 this is the best option, clean and g... Pin
AlbertoLeon CSharpMan2-Feb-12 10:50
AlbertoLeon CSharpMan2-Feb-12 10:50 
GeneralYou are right, it uses extension library 'PGK.Extensions' fr... Pin
Mikhail-T19-Jan-12 8:00
Mikhail-T19-Jan-12 8:00 
You are right, it uses extension library 'PGK.Extensions' from here: http://nuget.org/packages/PGK.Extensions/2011.6.
It's my fault, I forgot about that... But now I have mentioned it in this tip and also added your and Namlak's solution, which doesn't depend on that library! Thanks!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.