title: LINQ grouping of objects with Dictionary
body: <p>I have the following list of objects:</p>
  
  <pre><code>public class Record
  {
      public string SectionId { get; set; }
      public string SectionType { get; set; }
      public Dictionary&lt;string, List&lt;object&gt;&gt; RecordValues { get; set; }
  }
  
  var records = new List&lt;Record&gt;
          {
              new Record
              {
                  SectionId = "1",
                  SectionType = "H",
                  RecordValues = new Dictionary&lt;string, List&lt;object&gt;&gt; 
                  {
                      { "1", new List&lt;object&gt; { "Item 1", "Item 2" } }
                  }
              },
              new Record
              {
                  SectionId = "1",
                  SectionType = "H",
                  RecordValues = new Dictionary&lt;string, List&lt;object&gt;&gt; 
                  {
                      { "2", new List&lt;object&gt; { "Item 3", "Item 4" } }
                  }
              },
              new Record
              {
                  SectionId = "2",
                  SectionType = "T",
                  RecordValues = new Dictionary&lt;string, List&lt;object&gt;&gt;
                  {
                      { "1", new List&lt;object&gt; { "Item 5", "Item 6" } },
                  }
              },
              new Record
              {
                  SectionId = "2",
                  SectionType = "T",
                  RecordValues = new Dictionary&lt;string, List&lt;object&gt;&gt;
                  {
                      { "2", new List&lt;object&gt; { "Item 7", "Item 8" } }
                  }
              }
          }.AsEnumerable();
  </code></pre>
  
  <p>I need to create another list grouped by section from the list above. So I tried the following code but stuck with selecting Dictionary (see the line with ??? ).</p>
  
  <pre><code>public class Section
  {
      public string Id { get; set; }
      public string Type { get; set; }
      public Dictionary&lt;string, List&lt;object&gt;&gt; Records { get; set; }
  }
  
  var sections = records.GroupBy(g =&gt; new {g.SectionId, g.SectionType})
              .Select(s =&gt; new Section
              {
                  Id = s.Key.SectionId,
                  Type = s.Key.SectionType,
                  Records = s.Select(x =&gt; x.RecordValues).ToDictionary() // ???
              }).ToList();
  </code></pre>
  
  <p>What would be the correct code to assign Records property? Thanks.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I've made mistake in my code above for the records list initialization (changed now). 
  Each RecordValues dictionary in the Record object contains only single key value pair.
  So the goal is to group the RecordValues by section so the outcome from the Linq grouping should be like one below:</p>
  
  <pre><code>var sections = new List&lt;Section&gt;
          {
              new Section
              {
                  Id = "1",
                  Type = "H",
                  Records = new Dictionary&lt;string, List&lt;object&gt;&gt;
                  {
                      {"1", new List&lt;object&gt; {"Item 1", "Item 2"}},
                      {"2", new List&lt;object&gt; {"Item 3", "Item 4"}}
  
                  }
              },
              new Section
              {
                  Id = "2",
                  Type = "T",
                  Records = new Dictionary&lt;string, List&lt;object&gt;&gt;
                  {
                      {"1", new List&lt;object&gt; {"Item 5", "Item 6"}},
                      {"2", new List&lt;object&gt; {"Item 7", "Item 8"}}
                  }
              }
          }; 
  </code></pre>