title: Convert List<MyObject> to Dictionary <obj.string, List<obj.ID>>
body: <p>I would like to take a list of objects and convert it to a dictionary where the key is a field in the object, and the value is a list of a different field in the objects that match on the key. I can do this now with a loop but I feel this should be able to be accomplished with linq and not having to write the loop. I was thinking a combination of <code>GroupBy</code> and <code>ToDictionary</code> but have been unsuccessful so far.</p>
  
  <p>Here's how I'm doing it right now:</p>
  
  <pre><code>var samplesWithSpecificResult = new Dictionary&lt;string, List&lt;int&gt;&gt;();
  foreach(var sample in sampleList)
  {
      List&lt;int&gt; sampleIDs = null;
      if (samplesWithSpecificResult.TryGetValue(sample.ResultString, out sampleIDs))
      {
          sampleIDs.Add(sample.ID);
          continue;
      }
      sampleIDs = new List&lt;int&gt;();
      sampleIDs.Add(sample.ID);
      samplesWithSpecificResult.Add(sample.ResultString, sampleIDs);
  }
  </code></pre>
  
  <p>The farthest I can get with <code>.GroupBy().ToDictionay()</code> is  <code>Dictionary&lt;sample.ResultString, List&lt;sample&gt;&gt;</code>.</p>
  
  <p>Any help would be appreciated.</p>