title: Flattern Dictionary<string, HashSet<string>>
body: <p>I have been messing around with this for some days now, and I can't wrap my head around it:</p>
  
  <p>I have the following structure:</p>
  
  <pre><code>Dictionary&lt;string, HashSet&lt;string&gt;&gt;
  </code></pre>
  
  <p>Containing the following data:</p>
  
  <pre><code>P1     S1, S2
  P2     S1, S2
  </code></pre>
  
  <p>Where the P-prefixed values are dictionary keys and S-prefixed values are the HashSet values.</p>
  
  <p>What I need is the following output List, </p>
  
  <pre><code>P1, P2, S1, S2
  </code></pre>
  
  <p>In the case I would have the following startvalues :</p>
  
  <pre><code>P1     S1, S2
  P2     S1
  </code></pre>
  
  <p>The output List should be :</p>
  
  <pre><code>P1, P2, S1, P1, S2
  </code></pre>
  
  <p>If there similar values in the HashSet of several dictionary items, it should group them together, that's why P1, P2, have S1, P1 has only S2</p>
  
  <p>Here's a little TestApp that clarifies more:</p>
  
  <pre><code>static void Main(string[] args)
  {
      var start = new Dictionary&lt;string, HashSet&lt;string&gt;&gt;();
      var output = new List&lt;string&gt;();
  
      //example1
      start.Add("P1", new HashSet&lt;string&gt; { "S1", "S2" });
      start.Add("P2", new HashSet&lt;string&gt; { "S1", "S2" });
      output = HocusPocus(start);
      PrintResult(output); // should be P1, P2, S1, S2
  
      //example 2
      start.Clear();
      start.Add("P1", new HashSet&lt;string&gt; { "S1", "S2" });
      start.Add("P2", new HashSet&lt;string&gt; { "S1" });
      output = HocusPocus(start);
      PrintResult(output); // should be P1, P2, S1, P1, S2
  
      //example 3
      start.Clear();
      start.Add("P1", new HashSet&lt;string&gt; { "S1", "S2", "S3" });
      start.Add("P2", new HashSet&lt;string&gt; { "S1" });
      start.Add("P3", new HashSet&lt;string&gt; { "S1", "S2" });
      output = HocusPocus(start);
      PrintResult(output); // should be P1, P2, P3, S1, P1, P3, S2, P1, S3
  
      Console.ReadKey();
  }
  
  public static List&lt;string&gt; HocusPocus(Dictionary&lt;string, HashSet&lt;string&gt;&gt; data)
  {
      // magic happens here
  }
  
  public static void PrintResult(List&lt;string&gt; result)
  {
      result.ForEach(x =&gt; Console.Write($"{x},"));
      Console.WriteLine();
  }
  </code></pre>