title: LINQ GroupBy on multiple ref-type fields; Custom EqualityComparer
body: <p>So I've looked through about 20 examples on this on SO and elsewhere, but haven't found one which covers what I'm trying to do. This - <a href="https://stackoverflow.com/questions/188120/can-i-specify-my-explicit-type-comparator-inline">Can I specify my explicit type comparator inline?</a> - looks like what I need, but doesn't go far enough (or I don't understand how to take it further).</p>
  
  <ul>
  <li>I have a List of LoadData, the LoadData object has fields of both reference and value types</li>
  <li>Need to group on a mixture of ref and value fields, project the output to an anonymous type</li>
  <li><p>Need (I think) to provide a custom IEqualityComparer to specify how to compare the GroupBy fields, but they are an anonymous type</p>
  
  <pre><code>private class LoadData
  {
      public PeriodEndDto PeriodEnd { get; set; }
      public ComponentDto Component { get; set; }
      public string GroupCode { get; set; }
      public string PortfolioCode { get; set; }
  }
  </code></pre></li>
  </ul>
  
  <p>The best GroupBy query I have going so far:</p>
  
  <pre><code>var distinctLoads = list.GroupBy(
      dl =&gt; new { PeriodEnd = dl.PeriodEnd, 
                  Component = dl.Component, 
                  GroupCode = dl.GroupCode },
      (key, data) =&gt; new {PeriodEnd = key.PeriodEnd, 
                  Component = key.Component, 
                  GroupCode = key.GroupCode, 
                  PortfolioList = data.Select(d=&gt;d.PortfolioCode)
                                      .Aggregate((g1, g2) =&gt; g1 + "," + g2)},
      null);
  </code></pre>
  
  <p>This groups, but there are still duplicates.</p>
  
  <ol>
  <li>How can I specify custom code to compare the GroupBy fields?  For example, the Components could be compared by Component.Code.</li>
  </ol>