Click here to Skip to main content
15,878,748 members
Articles / General Programming / String
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.67/5 (8 votes)
24 Jan 2012CPOL1 min read 105.4K   8   5
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:

C#
@{
  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:

C#
@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:

C#
@{
  var cities_string = sourceData.Aggregate("", (a, b) => a + b.Name + ", ");
  cities_string = cities_string.Remove(cities_string.LastIndexOf(","));
}

@cities_string

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):
C#
@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!):
C#
@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[^].

License

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


Written By
Web Developer
United States United States
Coding is awesome!

Comments and Discussions

 
GeneralReason for my vote of 5 Nice! Basic stuff, but not necessari... Pin
yannduran17-Jan-12 16:10
yannduran17-Jan-12 16: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.