title: Grouping Nested KeyValue pairs to Dictionary
body: <p>I have the following code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Linq;
  
  
  public class Test
  {
      static void Main()
      {
  
          var list = new List&lt;KeyValuePair&lt;int, KeyValuePair&lt;int, User&gt;&gt;&gt;
                          {
                              new KeyValuePair&lt;int, KeyValuePair&lt;int, User&gt;&gt;(1,new KeyValuePair&lt;int, User&gt;(1,new User {FirstName = "Name1"})),
                              new KeyValuePair&lt;int, KeyValuePair&lt;int, User&gt;&gt;(1,new KeyValuePair&lt;int, User&gt;(1,new User {FirstName = "Name2"})),
                              new KeyValuePair&lt;int, KeyValuePair&lt;int, User&gt;&gt;(1,new KeyValuePair&lt;int, User&gt;(2,new User {FirstName = "Name3"})),
                              new KeyValuePair&lt;int, KeyValuePair&lt;int, User&gt;&gt;(1,new KeyValuePair&lt;int, User&gt;(2,new User {FirstName = "Name4"})),
                              new KeyValuePair&lt;int, KeyValuePair&lt;int, User&gt;&gt;(2,new KeyValuePair&lt;int, User&gt;(3,new User {FirstName = "Name5"})),
                              new KeyValuePair&lt;int, KeyValuePair&lt;int, User&gt;&gt;(2,new KeyValuePair&lt;int, User&gt;(3,new User {FirstName = "Name6"})),
                              new KeyValuePair&lt;int, KeyValuePair&lt;int, User&gt;&gt;(2,new KeyValuePair&lt;int, User&gt;(3,new User {FirstName = "Name6"})),
                              new KeyValuePair&lt;int, KeyValuePair&lt;int, User&gt;&gt;(3,new KeyValuePair&lt;int, User&gt;(4,new User {FirstName = "Name7"})),
                          };
      }
  }
  public class User
  {
      public string FirstName { get; set; }
  }
  </code></pre>
  
  <p>Above as you can see there are multiple values for same key for first KeyValue Pair and further there(in second nested key Value Pair) are multiple same keys  Now I want to group them and convert the list object to dictionary in which the Key will be the same(1,2 as shown above) but the first value will be dictionary and value for second will be the collection.Like this:</p>
  
  <pre><code>var outputNeeded = new Dictionary&lt;int,Dictionary&lt;int,Collection&lt;User&gt;&gt;&gt;();
  </code></pre>
  
  <p>How can I do it. ??</p>