Click here to Skip to main content
Click here to Skip to main content
Go to top

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(","));
}
 
@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):
@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[^].

License

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

Share

About the Author

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

Comments and Discussions

 
GeneralReason for my vote of 5 Is a common problem, not like all op... Pinmemberalbertoleon2-Feb-12 10:52 
GeneralReason for my vote of 5 Nice! Basic stuff, but not necessari... Pinmemberyannduran17-Jan-12 16:10 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 24 Jan 2012
Article Copyright 2012 by Mikhail-T
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid