title: How to convert Linq.IEnumerable(Of String) to formatted Strings
body: <p>I have a <code>List(Of String)</code> that stores validation errors.  Should that list contain any items, I'd like to concatenate them into an HTML list to show each error.  Currently this is easily done like so:</p>
  
  <pre><code>Dim l As List(Of String) = GetErrors()
  If l.Count &gt; 0 Then
      Dim sb As New StringBuilder
      sb.Append("&lt;div&gt;&lt;ul&gt;")
      For Each s As String In l
          sb.Append(String.Format("&lt;li&gt;{0}&lt;/li&gt;", s))
      Next
      sb.Append("&lt;/ul&gt;&lt;/div&gt;")
      ltl_status.Text = sb.ToString()
  End If
  </code></pre>
  
  <p>However, because this is quite lengthy, I wondered whether Linq could provide a shortcut.  I tried this (line breaks added for clarity):</p>
  
  <pre><code>If l.Count &gt; 0 Then
      ltl_status.Text = String.Format("&lt;div class=""failure""&gt;&lt;ul&gt;{0}&lt;/ul&gt;&lt;/div&gt;", 
          (
          From s As String In l Select 
              String.Format("&lt;li&gt;{0}&lt;/li&gt;", s)
          )
  )
  End If
  </code></pre>
  
  <p>However, given that IEnumerable is a collection, the end result is just this output in the Literal:</p>
  
  <pre><code>    System.Linq.Enumerable+WhereSelectListIterator`2[System.String,System.String]
  </code></pre>
  
  <p>The aim here is to build the list using the least possible lines of code.  I see that <code>String.Join</code> accepts an IEnumerable parameter, but that simply joins the items together, whereas here I need to add additional strings to the beginning and end of each item.  Is it possible?</p>
  
  <h2>Answer</h2>
  
  <p>Based on Jon Skeet's excellent suggestions, the extension method has saved me a <strong>ton</strong> of time and effort:</p>
  
  <pre><code>Public Module CollectionSignatureMethods
  
      ''' &lt;summary&gt;
      ''' Takes each String value in a String collection, reformats using a format, and then returns all as a single String.
      ''' &lt;/summary&gt;
      ''' &lt;param name="ie"&gt;An IEnumerable(Of String) collection of string values&lt;/param&gt;
      ''' &lt;param name="formatString"&gt;The string format (as per String.Format)&lt;/param&gt;
      ''' &lt;returns&gt;All of the Strings from the collection, reformatted and combined to a single String&lt;/returns&gt;
      ''' &lt;remarks&gt;Jon Skeet is the daddy(!)&lt;/remarks&gt;
      &lt;Extension()&gt; _
      Public Function JoinFormat(ie As IEnumerable(Of String), formatString As String) As String
          Return String.Join(String.Empty, ie.Select(Function(s As String) String.Format(formatString, s)))
      End Function
  
  End Module
  </code></pre>