Click here to Skip to main content
Click here to Skip to main content
Alternatives (5)

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

By , 24 Jan 2012

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

18 Jan 2012  
Namlak
 
string blah = string.Join(",", cities.Select(c=> c.Name));
24 Jan 2012  
Richard Deeming
 
If you're stuck with .NET 3.5, you can use the Aggregate extension method[^]:string cities_string = cities.Aggregate(new StringBuilder(), (sb, c) =>{ if (0 != sb.Length) sb.Append(", "); sb.Append(c.Name); return sb;}, sb => sb.ToString());
18 Jan 2012  
wgross
 
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...
4 Feb 2012  
Thornik
 
It seems that the author discovered the official documentation for LINQ! :)))All that you wrote is obvious as "2x2".Even more: when (sometime) you have to join multiple lines with separator and have no ability to use "string.join", I prefer this:cycle { res += ", " +...

License

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

About the Author

Mikhail-T
Web Developer
United States United States
MVC, .NET, AngularJS, JQuery, and POST/GET - all are my good friends!

| | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 24 Jan 2012
Article Copyright 2012 by Mikhail-T
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid