Click here to Skip to main content
12,395,871 members (64,028 online)
Click here to Skip to main content
Add your own
alternative version



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

, 24 Jan 2012 CPOL
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());


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

18 Jan 2012
string blah = string.Join(",", cities.Select(c=> c.Name));
18 Jan 2012
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...
24 Jan 2012
5 alternatives  
A short one-line way to convert Array or List of any data into custom string via LINQ


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


About the Author

Richard Deeming
Software Developer Nevalee Business Solutions
United Kingdom United Kingdom
No Biography provided

| | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 24 Jan 2012
Article Copyright 2012 by Richard Deeming
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid