An easy way to combine a list of strings into comma separated strings, where the strings are members of an object?

Say I have a list (tag) with a tag that is an object. One of the members of Tag, Tag.Description is a string, and I want to make comma-separated description elements.

Is there an easier way to do this than read the Description members in a List (Of String) and then use the Join function?

Thanks!

+4
source share
2 answers

Try the following:

String.Join(", ", tagList.Select(t => t.Description).ToArray()); 

Sorry, I just read and saw that you are using VS2005; therefore, it is best to create a StringBuilder and tag.Description .

+6
source

Here is the solution for Visual Studio 2005

 Public Function ConcatDescription(ByVal list As List(Of Tag) As String Dim builder as New StringBuilder Dim isFirst As Boolean = True For Each t As Tag in list If Not isFirst Then builder.Append(","c) End If builder.Append(t.Description) isFirst = False Next Return builder.ToString() End Function 
+5
source

All Articles