title: group by a dynamic composite key (variable length in the number of fields in the key)
body: <p>I need to group a list of items based on composite with variable length (i.e. the number of items in the composite key changes from case to case). I know how group by a fixed number of fields (using <code>Tuple</code>), but I can't figure out how to make the grouping composite key to take a variable length. I don't mean that it should change the length during the same "grouping stage". I would like to write it once and use it dynamically (a better alternative to writing <em>i</em> methods that take a different number of fields as keys.</p>
  
  <p>Consider this simple class:</p>
  
  <pre><code>class SimpleItem
  {
      public string TargetField { get; set; }
      public string JoinField { get; set; }
      public string InformationField { get; set; }
      public string Additional { get; set; }
  }
  </code></pre>
  
  <p>I can group by multiple keys by using something like:</p>
  
  <pre><code>var grouped = simpleList.GroupBy(x =&gt; new Tuple&lt;string, string&gt;(x.JoinField, x.TargetField))
               .ToDictionary(x =&gt; x.Key, y =&gt; y.ToList());
  </code></pre>
  
  <p>How can I make this grouping, more specifically this key generation <code>new Tuple&lt;string, string&gt;(x.JoinField, x.TargetField)</code>, dynamic? I could have a variable number of fields (in this example from 1 to 4) in the composite key and also different field combinations (a total of 12 possibilities, if I'm not mistaking). Is it even possible?</p>
  
  <p><strong>EDIT:</strong> I have the freedom of changing all the fields into a single <code>Dictionary&lt;string, string&gt;</code>, i.e. have a single dictionary property that stores all other previous properties.</p>