title: Why does using anonymous type work and using an explicit type not in a GroupBy?
body: <p>I have a problem where I want a group type to be strongly typed but if I do it doesn't group correctly. See the code below...</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace ConsoleApplication35
  {
      class Program
      {
          static void Main(string[] args)
          {
              List&lt;Foo&gt; foos = new List&lt;Foo&gt;();
              foos.Add(new Foo() { Key = "Test" });
              foos.Add(new Foo() { Key = "Test" });
              foos.Add(new Foo() { Key = "Test" });
  
              var groups = foos.GroupBy&lt;Foo, dynamic&gt;(entry =&gt; new
              {
                  GroupKey = entry.Key
              });
  
              Console.WriteLine(groups.Count());
  
              groups = foos.GroupBy&lt;Foo, dynamic&gt;(entry =&gt; new GroupingKey()
              {
                  GroupKey = entry.Key
              });
  
              Console.WriteLine(groups.Count());
  
          }
  
          public class Foo
          {
              public string Key { get; set; }
          }
  
          public class GroupingKey
          {
              public string GroupKey { get; set; }
          }
      } 
  }
  </code></pre>
  
  <p>The output:</p>
  
  <pre><code>1
  3
  Press any key to continue . . .
  </code></pre>
  
  <p>I would expect the result to be the same regardless of using an explicit type nor not i.e. should only be one group with 3 items not 3 groups with 1 item. What is going on here?</p>
  
  <p><em><strong>Update</em></strong>
  I added an IEqualityComparer and it works now! See below:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace ConsoleApplication35
  {
      class Program
      {
          static void Main(string[] args)
          {
              List&lt;Foo&gt; foos = new List&lt;Foo&gt;();
              foos.Add(new Foo() { Key = "Test" });
              foos.Add(new Foo() { Key = "Test" });
              foos.Add(new Foo() { Key = "Test" });
  
              var groups = foos.GroupBy&lt;Foo, dynamic&gt;(entry =&gt; new //GroupingKey()
              {
                  GroupKey = entry.Key
              });
  
              Console.WriteLine(groups.Count());
  
              groups = foos.GroupBy&lt;Foo, GroupingKey&gt;(entry =&gt; new GroupingKey()
              {
                  GroupKey = entry.Key
              }, new GroupingKeyEqualityComparer());
  
              Console.WriteLine(groups.Count());
  
          }
  
          public class Foo
          {
              public string Key { get; set; }
          }
  
          public class GroupingKey
          {
              public string GroupKey { get; set; }              
          }
  
          public class GroupingKeyEqualityComparer : IEqualityComparer&lt;GroupingKey&gt;
          {
              #region IEqualityComparer&lt;GroupingKey&gt; Members
  
              public bool Equals(GroupingKey x, GroupingKey y)
              {
                  return x.GroupKey == y.GroupKey;
              }
  
              public int GetHashCode(GroupingKey obj)
              {
                  return obj.GroupKey.GetHashCode();
              }
  
              #endregion
          }
      } 
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>1
  1
  Press any key to continue . . .
  </code></pre>
  
  <p>This pretty much confirms the answer given by JaredPar!</p>