title: How to output a sorted dictionary to CSV?
body: <p>I have a sorted dictionary as in the following picture:</p>
  
  <pre><code>  var enteries = new SortedDictionary&lt;IgaAdKey, IgaEntry&gt;();
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/PCeZQ.png" alt="enter image description here"></p>
  
  <p>I would like to print item 0, then item 2, then item 4, in one line, CSV format, then go to item 1, 3, 5 and print them in on CSV line. 
  But there are cases where there only 3 unique resources, or just 3 unique <code>ResourceId</code> values and just one different <code>ResourceId</code>. 
  At these cases I want to print an empty row by writing 5 commas (",,,,,").</p>
  
  <p>Here is how I'm doing it but of course I'm skipping new lines</p>
  
  <pre><code>   foreach (var pair in enteries.OrderBy(pair =&gt; pair.Key.ResourceId))
      {
          if (!currResources.Contains(pair.Key.ResourceId))
          {
              currResources.Add(pair.Key.ResourceId);
  
              IgaEntry entry = enteries[pair.Key];
  
              streamWriter.Write(pair.Key.LocationId + ",");
              streamWriter.Write(pair.Key.EditionId + ",");
              streamWriter.Write(entry.mClickCount + ",");
              streamWriter.Write(entry.mViewCount + ",");
              streamWriter.Write(",");
          }
          else
          {
              IgaEntry entry = enteries[pair.Key];
  
              streamWriter.Write("\n");
              streamWriter.Write(date.ToString("yyyyMMdd") + ",");
  
              int search = resourcesList.IndexOf(pair.Key.ResourceId);
              if (search &gt; 0)
              {
                  for (int i = 0; i &lt; search * 5; i++)
                  {
                      streamWriter.Write(",");
                  }
              }
              streamWriter.Write(pair.Key.LocationId + ",");
              streamWriter.Write(pair.Key.EditionId + ",");
              streamWriter.Write(entry.mClickCount + ",");
              streamWriter.Write(entry.mViewCount + ",");
              streamWriter.Write(",");
          }
      }
  
  }
  </code></pre>
  
  <p>The result of that code is :</p>
  
  <p><img src="https://i.stack.imgur.com/QdhAj.png" alt="enter image description here"></p>
  
  <p>and it should be like that :</p>
  
  <p><img src="https://i.stack.imgur.com/p79p6.png" alt="enter image description here"></p>