title: Entity Framework/Linq to SQL: Skip & Take
body: <p>Just curious as to how Skip &amp; Take are supposed to work. I'm getting the results I want to see on the client side, but when I hook up the AnjLab SQL Profiler and look at the SQL that is being executed it looks as though it is querying for and returning the entire set of rows to the client.</p>
  
  <p>Is it really returning all the rows then sorting and narrowing down stuff with LINQ on the client side?</p>
  
  <p>I've tried doing it with both Entity Framework and Linq to SQL; both appear to have the same behavior.</p>
  
  <p>Not sure it makes any difference, but I'm using C# in VWD 2010.</p>
  
  <p>Any insight?</p>
  
  <pre><code>public IEnumerable&lt;Store&gt; ListStores(Func&lt;Store, string&gt; sort, bool desc, int page, int pageSize, out int totalRecords)
  {
      var context = new TectonicEntities();
      totalRecords = context.Stores.Count();
      int skipRows = (page - 1) * pageSize;
      if (desc)
          return context.Stores.OrderByDescending(sort).Skip(skipRows).Take(pageSize).ToList();
      return context.Stores.OrderBy(sort).Skip(skipRows).Take(pageSize).ToList();
  }
  </code></pre>
  
  <p>Resulting SQL (Note: I'm excluding the Count query):</p>
  
  <pre><code>SELECT 
  [Extent1].[ID] AS [ID], 
  [Extent1].[Name] AS [Name], 
  [Extent1].[LegalName] AS [LegalName], 
  [Extent1].[YearEstablished] AS [YearEstablished], 
  [Extent1].[DiskPath] AS [DiskPath], 
  [Extent1].[URL] AS [URL], 
  [Extent1].[SecureURL] AS [SecureURL], 
  [Extent1].[UseSSL] AS [UseSSL]
  FROM [dbo].[tec_Stores] AS [Extent1]
  </code></pre>
  
  <p>After some further research, I found that the following works the way I would expect it to:</p>
  
  <pre><code>public IEnumerable&lt;Store&gt; ListStores(Func&lt;Store, string&gt; sort, bool desc, int page, int pageSize, out int totalRecords)
  {
      var context = new TectonicEntities();
      totalRecords = context.Stores.Count();
      int skipRows = (page - 1) * pageSize;           
      var qry = from s in context.Stores orderby s.Name ascending select s;
      return qry.Skip(skipRows).Take(pageSize);           
  }
  </code></pre>
  
  <p>Resulting SQL:</p>
  
  <pre><code>SELECT TOP (3) 
  [Extent1].[ID] AS [ID], 
  [Extent1].[Name] AS [Name], 
  [Extent1].[LegalName] AS [LegalName], 
  [Extent1].[YearEstablished] AS [YearEstablished], 
  [Extent1].[DiskPath] AS [DiskPath], 
  [Extent1].[URL] AS [URL], 
  [Extent1].[SecureURL] AS [SecureURL], 
  [Extent1].[UseSSL] AS [UseSSL]
  FROM ( SELECT [Extent1].[ID] AS [ID], [Extent1].[Name] AS [Name], [Extent1].[LegalName] AS [LegalName], [Extent1].[YearEstablished] AS [YearEstablished], [Extent1].[DiskPath] AS [DiskPath], [Extent1].[URL] AS [URL], [Extent1].[SecureURL] AS [SecureURL], [Extent1].[UseSSL] AS [UseSSL], row_number() OVER (ORDER BY [Extent1].[Name] ASC) AS [row_number]
      FROM [dbo].[tec_Stores] AS [Extent1]
  )  AS [Extent1]
  WHERE [Extent1].[row_number] &gt; 3
  ORDER BY [Extent1].[Name] ASC
  </code></pre>
  
  <p>I really like the way the first option works; Passing in a lambda expression for sort. Is there any way to accomplish the same thing in the LINQ to SQL orderby syntax?  I tried using qry.OrderBy(sort).Skip(skipRows).Take(pageSize), but that ended up giving me the same results as my first block of code. Leads me to believe my issues are somehow tied to OrderBy.</p>
  
  <p>====================================</p>
  
  <h1>PROBLEM SOLVED</h1>
  
  <p>Had to wrap the incoming lambda function in Expression: </p>
  
  <pre><code>Expression&lt;Func&lt;Store,string&gt;&gt; sort
  </code></pre>