I have an object
public class Title
{
public int Id {get; set; }
public string Title {get; set; }
}
How to join all Title with "-" in a List<Title> ?
I think this should give you what you're looking for. This will select the Title property from each object into a string array, and then join all elements of that array into a '-' separated string.
List<Title> lst = new List<Title>
{
new Title{Id = 1, Title = "title1"},
new Title{Id = 2, Title = "title2"}
}
String.Join("-", lst.Select(x => x.Title).ToArray());
If you're using .NET 4.0 or later, there is now an overload to String.Join that will allow you to omit the .ToArray():
String.Join("-", lst.Select(x => x.Title));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With