title: ILookup<TKey, TVal> vs. IGrouping<TKey, TVal>
body: <p>I've been having trouble articulating the differences between <a href="http://msdn.microsoft.com/en-us/library/bb534291.aspx"><code>ILookup&lt;TKey, TVal&gt;</code></a> and <a href="http://msdn.microsoft.com/en-us/library/bb344977.aspx"><code>IGrouping&lt;TKey, TVal&gt;</code></a>, and am curious if I understand it correctly now.  LINQ compounded the issue by producing sequences of <code>IGrouping</code> items while also giving me a <code>ToLookup</code> extension method. So it felt like they were the same until I looked more closely.</p>
  
  <pre><code>var q1 = 
      from n in N
      group n by n.MyKey into g
      select g;
  // q1 is IEnumerable&lt;IGrouping&lt;TKey, TVal&gt;&gt;
  </code></pre>
  
  <p>Which is equivalent to:</p>
  
  <pre><code>var q2 = N.GroupBy(n =&gt; n.MyKey, n =&gt; n);
  // q2 is IEnumerable&lt;IGrouping&lt;TKey, TVal&gt;&gt;
  </code></pre>
  
  <p>Which looks a lot like:</p>
  
  <pre><code>var q3 = N.ToLookup(n =&gt; n.MyKey, n =&gt; n);
  // q3 is ILookup&lt;TKey, TVal&gt;
  </code></pre>
  
  <p>Am I correct in the following analogies?</p>
  
  <ol>
  <li>An <code>IGrouping&lt;TKey, TVal&gt;</code> is a single group (i.e. a keyed sequence), analogous to <code>KeyValuePair&lt;TKey, TVal&gt;</code> where the value is actually a sequence of elements (rather than a single element)</li>
  <li>An <code>IEnumerable&lt;IGrouping&lt;TKey, TVal&gt;&gt;</code> is a sequence of those (similar to what you get when iterating over an <code>IDictionary&lt;TKey, TVal&gt;</code></li>
  <li>An <code>ILookup&lt;TKey, TVal&gt;</code> is more like a <code>IDictionary&lt;TKey, TVal&gt;</code> where the value is actually a sequence of elements</li>
  </ol>