Click here to Skip to main content
13,833,525 members
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 17 Jan 2012
Licenced CPOL

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

, 24 Jan 2012
Rate this:
Please Sign up or sign in to vote.
A short one-line way to convert Array or List of any data into custom string via LINQ
Have you ever needed to convert an Array or any sort of list to a string containing some values which were then divided by, let's say, commas (",")? I think most developers did.

To do that, you could use a For or Foreach cycle, which is probably ugly and takes many lines, especially if you just need to list some names, e.g. "Name1, Name2, ... NameN".

A better and cleaner way is to use some LINQ statements to do that in C#. I will show an example below, and lastly I will show the easier and cleaner way to do so!

Given, we have a list of city names:

  var cities = new[] {
    new {Name = "Monroe", Id = 1},
    new {Name = "Moscow", Id = 2},
    new {Name = "New Orleans", Id = 3},
    new {Name = "Ottawa", Id = 4},
    new {Name = "Mumbai", Id = 5},
    new {Name = "Rome", Id = 6},
    new {Name = "Rio", Id = 7}

So to convert it to string of names via LINQ, you could do that:

@cities.Aggregate("", (a, b) => a + b.Name + ", ")

And that will return: "Monroe, Moscow, New Orleans, Ottawa, Mumbai, Rome, Rio,"

It's just what we need... Or is it? Did you notice that ugly comma (",") after the Rio? Don't you just hate it, ha?

So, to remove it, you might want to do something similar:

  var cities_string = sourceData.Aggregate("", (a, b) => a + b.Name + ", ");
  cities_string = cities_string.Remove(cities_string.LastIndexOf(","));


Which will work, but let's admit that it would be a bit ugly for such a simple operation.

But, good news! You can do it in one line, which I call 'The Nice Way'))
(NOTE: To do that, you need PGK.Extensions[^] extension library package):
@cities.Select(x => x.Name).ToList().Join(", ")

Alternatively, if you don't want to install any extensions, you can use string extension built-in into .NET 4.0 (suggested by Namlak[^] & wgross[^], thanks!):
@string.Join(", ", cities.Select(c => c.Name))

Both methods return: "Monroe, Moscow, New Orleans, Ottawa, Mumbai, Rome, Rio"

And that's it! No need to remove that ugly comma anymore!

P.S. If you have to use .NET 3.5, then you'll have to do via .Aggregate extension method as Richard[^] has suggested in Alternative 3[^].


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


About the Author

Web Developer
United States United States
Coding is awesome!

You may also be interested in...

Comments and Discussions

QuestionMessage Closed Pin
20-Mar-15 4:38
memberMember 1152781420-Mar-15 4:38 
GeneralReason for my vote of 5 Is a common problem, not like all op... Pin
AlbertoLeon CSharpMan2-Feb-12 11:52
memberAlbertoLeon CSharpMan2-Feb-12 11:52 
GeneralReason for my vote of 5 Nice! Basic stuff, but not necessari... Pin
yannduran17-Jan-12 17:10
memberyannduran17-Jan-12 17:10 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190114.1 | Last Updated 24 Jan 2012
Article Copyright 2012 by Mikhail-T
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid