tags:

views:

91

answers:

3

Given:

    protected class Marker { 
        public string Name { get; set; }
        public string Lat { get; set; }
        public string Long { get; set; } 
    };

List<Marker> allMarkers = new List<Marker>();
allMarkers.Add(new Marker{Name="Bondi Beach", Lat = "-33.890542", Long = "151.274856"});
allMarkers.Add(new Marker{Name="Coogee Beach", Lat = "-33.923036", Long = "151.259052"});
allMarkers.Add(new Marker{Name="Cronulla Beach", Lat = "-34.028249", Long = "151.157507"});
allMarkers.Add(new Marker{Name="Manly Beach", Lat = "-33.800101", Long = "151.287478"});
allMarkers.Add(new Marker{Name="Maroubra Beach", Lat = "-33.950198", Long = "151.259302"});

I'd like to convert to a string in this format:

['Bondi Beach', -33.890542, 151.274856],
['Coogee Beach', -33.923036, 151.259052],
['Cronulla Beach', -34.028249, 151.157507],
['Manly Beach', -33.800101, 151.287478],
['Maroubra Beach', -33.950198, 151.259302]

Is there a one liner way to do this, something similar to string.Join(), or do I have to do it manually via a foreach on the List and use stringbuilder.appendformat()?

+7  A: 

If Marker is your own class, consider overriding the ToString() method to display each line the way you do. Then, you can use a simple String.Join() to combine it all together.

protected class Marker
{
    public string Name { get; set; }
    public string Lat { get; set; }
    public string Long { get; set; }
    public override string ToString()
    {
        return String.Format("['{0}', {1}, {2}]", Name, Lat, Long);
    }
};

Then, to use:

List<Marker> allMarkers = new List<Marker>();
allMarkers.Add(new Marker { Name = "Bondi Beach", Lat = "-33.890542", Long = "151.274856" });
allMarkers.Add(new Marker { Name = "Coogee Beach", Lat = "-33.923036", Long = "151.259052" });
allMarkers.Add(new Marker { Name = "Cronulla Beach", Lat = "-34.028249", Long = "151.157507" });
allMarkers.Add(new Marker { Name = "Manly Beach", Lat = "-33.800101", Long = "151.287478" });
allMarkers.Add(new Marker { Name = "Maroubra Beach", Lat = "-33.950198", Long = "151.259302" });
Console.Write(String.Join(",\n", allMarkers));

Note: If you are dealing with a lot of markers and notice bad performance, consider rewriting that String.Format() line as:

return "['" + Name + "', " + Lat.ToString() + ", " + Long.ToString() + "]";

You may notice this is better (or worse), depending on your use case.

drharris
Or write an extension method for it if it's not your class
Eric
+1. Deleting my answer. Didn't know about string.Join. :-P
StriplingWarrior
+1. Excellent use of String.Join()
mpenrow
It's useful to note that this is using a .NET 4 only overload of `string.Join`. 3.5 will require conversion of the list to a string array. Also, collection initializers allow you to somewhat reduce the ceremony of priming this list with sample data.
Marc
Thanks Marc, coincidentally this is my first 4.0 project so it worked for me. :)
tbone
A: 

By using extension methods, you can do something like this

static class Extensions
{
   static string ToStringAll(this List<Marker> markers)
   {
      string all = "";

      foreach (var marker in markers)
      {
         all += marker.ToString() + ",";
      }

      return all.Trim(',');
   }
}

Then all you need to use in the future is

allMarkers.ToStringAll();
Johan
This is going to kill performance if there are a lot of markers. Strings are not mutable in C#.
drharris
+4  A: 

You can use String.Join and a simple LINQ Select to project them to the format you wish ...

   allMarkers.Select(m => "['" + m.Name + "', " + m.Lat + ", " + m.Long + "]")

BUT You might instead want to consider serializing your data to JSON using DataContract serialization rather than inventing your own serialized object notation.

Hightechrider
string.Join(",\n", allMarkers.Select(m => "['" + m.Name + "', " + m.Lat + ", " + m.Long + "]")); I like it.
tbone