title: Lazy combinations
body: <p>I'm looking for a more lazy/IEnumerable/cleaner way of doing the following. I particularly not happy with the use of <code>helper</code> and Aggregate.</p>
  
  <p>Any hints on how to modify the code to make it possible?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace Test1
  {
      class Program
      {
          static void PrintOut&lt;T&gt;(IEnumerable&lt;IEnumerable&lt;T&gt;&gt; data)
          {
              foreach (var item in data)
              {
                  string output = "-";
                  if (item != null)
                      output = string.Join(",", item.Select(x =&gt; (x == null) ? "-" : x.ToString()));
                  Console.WriteLine(output);
              }
          }
  
  
          static IEnumerable&lt;T&gt; helper&lt;T&gt;(IEnumerable&lt;T&gt; orig, T toAdd)
          {
              if (orig != null)
                  foreach (var item in orig)
                      yield return item;
              yield return toAdd;
              yield break;
          }
  
  
          static IEnumerable&lt;IEnumerable&lt;T&gt;&gt; helper2&lt;T&gt;(IEnumerable&lt;IEnumerable&lt;T&gt;&gt; input) where T : class
          {
              var initalAcc = new List&lt;IEnumerable&lt;T&gt;&gt; { };
              var result = input.Aggregate(initalAcc, 
                  (acc, choiceSet) =&gt;
                      acc.DefaultIfEmpty()
                          .SelectMany((chosen) =&gt; (choiceSet ?? new List&lt;T&gt; { }).DefaultIfEmpty().Select(choice =&gt; helper(chosen, choice))).ToList()
              );
              return result;
          }
  
          static void Main(string[] args)
          {
              var preCombination = new List&lt;List&lt;string&gt;&gt; { 
                  new List&lt;string&gt; {"1","2"}, 
                  new List&lt;string&gt; {"3"},
                  new List&lt;string&gt; {"4","5"},
                  null,
                  new List&lt;string&gt; {"6","7"}
              };
              var postCombination = helper2(preCombination);
  
              PrintOut(preCombination);
              Console.WriteLine();
              PrintOut(postCombination);
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p>Here is the expected output</p>
  
  <pre><code>1,2
  3
  4,5
  -
  6,7
  
  1,3,4,-,6
  1,3,4,-,7
  1,3,5,-,6
  1,3,5,-,7
  2,3,4,-,6
  2,3,4,-,7
  2,3,5,-,6
  2,3,5,-,7
  </code></pre>
  
  <p>I've changed initalAcc now</p>
  
  <pre><code>var initalAcc = Enumerable.Empty&lt;IEnumerable&lt;T&gt;&gt;();
  </code></pre>