title: Entity framework - COUNT rather than SELECT
body: <p>If I call the <code>GetFoo().Count()</code> from a method outside of my DB class, Entity Framework 5 does a rather inefficient SELECT query rather than a COUNT. From reading a few other questions like <a href="https://stackoverflow.com/questions/890381/how-to-count-rows-within-entityframework-without-loading-contents">this</a>,  I see that this is expected behaviour.</p>
  
  <pre><code>public IEnumerable&lt;DbItems&gt; GetFoo()
  {
      return context.Items.Where(d =&gt; d.Foo.equals("bar"));
  }
  </code></pre>
  
  <p>I've therefore added a count method to my DB class, which correctly performs a COUNT query:</p>
  
  <pre><code>public int GetFooCount()
  {
      return context.Items.Where(d =&gt; d.Foo.equals("bar")).Count();
  }
  </code></pre>
  
  <p>To save me from specifying queries multiple times, I'd like to change this to the following. However this again performs a SELECT, even though it's within the DB class. Why is this - and how can I avoid it?</p>
  
  <pre><code>public int GetFooCount()
  {
      return this.GetFoo().Count();
  }
  </code></pre>