Convert items in List<> to a concatenated and formatted string?
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()?
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.
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.
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();
精彩评论