title: Entity framework paging with extension method is slow?
body: <p>I have some problems with a slow query in Entity Framework in C#. I have created an extension method called Page to handle paging, but when I use it the query gets really slow. If I just do  .Skip(page.Value * pageSize.Value).Take(pageSize.Value) instead of using Page the query gets a lot faster. I guess that doing it with Page fetches all contacts before paging. Is there a way to prevent this or am I doing something else wrong?</p>
  
  <p>Query:</p>
  
  <pre><code>var contacts = db.Contacts
                          .Where(x =&gt; x.AccountID == accountID &amp;&amp; x.Deleted == false)
                          .OrderByDescending(x =&gt; x.FirstName)
                          .ThenBy(x =&gt; x.LastName)
                          .ThenBy(x =&gt; x.CreatedDate)
                          .Page(page, pageSize);
  
  return contacts.ToList();
  </code></pre>
  
  <p>Extension method:</p>
  
  <pre><code>    public static IEnumerable&lt;T&gt; Page&lt;T&gt;(this IEnumerable&lt;T&gt; elements, int? page, int? pageSize)
      {
          if (page.HasValue &amp;&amp; pageSize.HasValue)
              return elements.Skip(page.Value * pageSize.Value).Take(pageSize.Value);
          else
              return elements;
      }
  </code></pre>