title: Using Enumerable.Aggregate(...) Method over an empty sequence
body: <p>I would like to use the <em>Enumerable.Aggregate(...)</em> method to concatenate a list of strings separated by a semicolon. Rather easy, isn't it?</p>
  
  <p>Considering the following:</p>
  
  <ul>
  <li><code>private const string LISTSEPARATOR = "; ";</code></li>
  <li><em>album.<strong>OrderedTracks</em></strong> is <code>List&lt;TrackDetails&gt;</code></li>
  <li><em>TrackDetails</em> has <em>DiscNumber</em> Int16? property</li>
  </ul>
  
  <p>The following statement will trow an exception if the sequence returned by <em>Distinct()</em> is empty (as the <em>Aggregate()</em> method doesn't apply on empty sequence):</p>
  
  <pre><code>    txtDiscNumber.Text = album.OrderedTracks
          .Where(a =&gt; a.DiscNumber.HasValue)
          .Select(a =&gt; a.DiscNumber.Value.ToString())
          .Distinct()
          .Aggregate((i, j) =&gt; i + LISTSEPARATOR + j);
  </code></pre>
  
  <p>The workaround I am using:</p>
  
  <pre><code>    List&lt;string&gt; DiscNumbers = 
          album.OrderedTracks
              .Where(a =&gt; a.DiscNumber.HasValue)
              .Select(a =&gt; a.DiscNumber.Value.ToString())
              .Distinct()
              .ToList();
  
      if (!DiscNumbers.Any())
          txtDiscNumber.Text = null;
      else
          txtDiscNumber.Text = 
              DiscNumbers.Aggregate((i, j) =&gt; i + LISTSEPARATOR + j);
  </code></pre>
  
  <p>Is there any better solution? Is it possible to do this in a single LINQ statement?</p>
  
  <p>Thanks in advance.</p>