title: The method ‘Skip’ is only supported for sorted input in LINQ to Entities. The method ‘OrderBy’ must be called before the method ‘Skip’
body: <p>Using <strong>Entity Framework 6.0.2</strong> and <strong>.NET 4.5.1</strong> in <strong>Visual Studio 2013 Update 1</strong> with a <code>DbContext</code> connected to SQL Server:</p>
  
  <p>I have a long chain of filters I am applying to a query based on the caller's desired results.  Everything was fine until I needed to add paging.  Here's a glimpse:</p>
  
  <pre><code>IQueryable&lt;ProviderWithDistance&gt; results = (from pl in db.ProviderLocations
                                              let distance = pl.Location.Geocode.Distance(_geo)
                                              where pl.Location.Geocode.IsEmpty == false
                                              where distance &lt;= radius * 1609.344
                                              orderby distance
                                              select new ProviderWithDistance() { Provider = pl.Provider, Distance = Math.Round((double)(distance / 1609.344), 1) }).Distinct();
  
  if (gender != null)
  {
      results = results.Where(p =&gt; p.Provider.Gender == (gender.ToUpper() == "M" ? Gender.Male : Gender.Female));
  }
  
  if (type != null)
  {
      int providerType;
      if (int.TryParse(type, out providerType))
          results = results.Where(p =&gt; p.Provider.ProviderType.Id == providerType);
  }
  
  if (newpatients != null &amp;&amp; newpatients == true)
  {
      results = results.Where(p =&gt; p.Provider.ProviderLocations.Any(pl =&gt; pl.AcceptingNewPatients == null || pl.AcceptingNewPatients == AcceptingNewPatients.Yes));
  }
  
  if (string.IsNullOrEmpty(specialties) == false)
  {
      List&lt;int&gt; _ids = specialties.Split(',').Select(int.Parse).ToList();
  
      results = results.Where(p =&gt; p.Provider.Specialties.Any(x =&gt; _ids.Contains(x.Id)));
  }
  
  if (string.IsNullOrEmpty(degrees) == false)
  {
      List&lt;int&gt; _ids = specialties.Split(',').Select(int.Parse).ToList();
  
      results = results.Where(p =&gt; p.Provider.Degrees.Any(x =&gt; _ids.Contains(x.Id)));
  }
  
  if (string.IsNullOrEmpty(languages) == false)
  {
      List&lt;int&gt; _ids = specialties.Split(',').Select(int.Parse).ToList();
  
      results = results.Where(p =&gt; p.Provider.Languages.Any(x =&gt; _ids.Contains(x.Id)));
  }
  
  if (string.IsNullOrEmpty(keyword) == false)
  {
      results = results.Where(p =&gt;
          (p.Provider.FirstName + " " + p.Provider.LastName).Contains(keyword));
  }
  </code></pre>
  
  <p>Here's the paging I added to the bottom (<code>skip</code> and <code>max</code> are just <strong>int</strong> parameters):</p>
  
  <pre><code>if (skip &gt; 0)
      results = results.Skip(skip);
  
  results = results.Take(max);
  
  return new ProviderWithDistanceDto { Locations = results.AsEnumerable() };
  </code></pre>
  
  <p>Now for my question(s):</p>
  
  <ol>
  <li><p>As you can see, I am doing an <strong>orderby</strong> in the initial LINQ query, so why is it complaining that I need to do an <code>OrderBy</code> before doing a <code>Skip</code> (I thought I was?)...</p></li>
  <li><p>I was under the assumption that it won't be turned into a SQL query and executed until I enumerate the results, which is why I wait until the last line to return the results <strong>AsEnumerable()</strong>.  Is that the correct approach?</p></li>
  <li><p>If I have to enumerate the results before doing <code>Skip</code> and <code>Take</code> how will that affect performance? Obviously I'd like to have SQL Server do the heavy lifting and return only the requested results. Or does it not matter (or have I got it wrong)?</p></li>
  </ol>