title: Count and Group By an Entity (Not an Entity's Property)
body: <p>I have the following entity model. I omitted a lot of properties on each entity for brevity's sake.</p>
  
  <pre><code>public sealed class Platform {
      /// &lt;summary&gt;
      /// Get and Set Platform's Unique Identifier.
      /// &lt;/summary&gt;
      public int Id { get; set; }
  
      /// &lt;summary&gt;
      /// Determine if an Object is Equal to This Platform.
      /// &lt;/summary&gt;
      /// &lt;param name="obj"&gt;
      /// An object to compare.
      /// &lt;/param&gt;
      /// &lt;returns&gt;
      /// A boolean true if the object is equal to this platform. A boolean false otherwise.
      /// &lt;/returns&gt;
      public override bool Equals(object obj) {
          bool isObjectPlatform = obj is Platform;
          bool isObjectIdEqual = isObjectPlatform &amp;&amp; (obj as Platform).Id == this.Id;
  
          return isObjectIdEqual;
      }
  
      /// &lt;summary&gt;
      /// Get Platform's Hash Code.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;
      /// The platform's hash code, equalling the platform's unique identifier.
      /// &lt;/returns&gt;
      public override int GetHashCode() {
          return this.Id;
      }
  }
  
  public sealed class Capture {
      /// &lt;summary&gt;
      /// Get and Set Capture's Unique Identifier.
      /// &lt;/summary&gt;
      public int Id { get; set; }
  
      /// &lt;summary&gt;
      /// Get and Set Capture's Platform.
      /// &lt;/summary&gt;
      public Platform Platform { get; set; }
  
      /// &lt;summary&gt;
      /// Determine if an Object is Equal to This Capture.
      /// &lt;/summary&gt;
      /// &lt;param name="obj"&gt;
      /// An object to compare.
      /// &lt;/param&gt;
      /// &lt;returns&gt;
      /// A boolean true if the object is equal to this capture. A boolean false otherwise.
      /// &lt;/returns&gt;
      public override bool Equals(object obj) {
          bool isObjectCapture = obj is Capture;
          bool isObjectIdEqual = isObjectCapture &amp;&amp; (obj as Capture).Id == this.Id;
  
          return isObjectIdEqual;
      }
  
      /// &lt;summary&gt;
      /// Get Capture's Hash Code.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;
      /// The capture's hash code, equalling the capture's unique identifier.
      /// &lt;/returns&gt;
      public override int GetHashCode() {
          return this.Id;
      }
  }
  </code></pre>
  
  <p>What I basically want is a LINQ query, backed up by an EF of course, that will get me the count of <strong>Captures</strong> grouped by an entire <strong>Platform</strong> entity. I want to be able to return a reference to each <strong>Platform</strong>, instead of one of its properties, and the count of each <strong>Capture</strong> associated with it.</p>
  
  <p>I did this query and it works great:</p>
  
  <pre><code>var query = this._defaultContext.Captures
                  .Include(m =&gt; m.Platform)
                  .GroupBy(m =&gt; m.Platform.Id)
                  .Select(m =&gt; new {
                      PlatformId = m.Key,
                      Count = m.Count()
                  });
  </code></pre>
  
  <p>But as you can see, I am grouping by the <strong>Platform's</strong> <em>Id</em> property. What I would rather have is this:</p>
  
  <pre><code>var query = this._defaultContext.Captures
                  .Include(m =&gt; m.Platform)
                  .GroupBy(m =&gt; m.Platform)
                  .Select(m =&gt; new {
                      Platform = m.Key,
                      Count = m.Count()
                  });
  </code></pre>
  
  <p>This does not work. It just gets a count of 1 for each record in the database. It looks it does not know to group by an entire entity. I was hoping it could by differentiating between each <strong>Platform</strong> by way of the <strong>GetHashCode</strong> and <strong>Equals</strong> method implementations but no luck.</p>
  
  <p>Anyone ever run into a scenario like this? Any way around it? Or do I have to manually do it. I would hate that since it would probably result in some kind of N + 1 query.</p>
  
  <p>Thanks in advance.</p>