title: How to cast an IQueryable to a type List (Of Orders)
body: <p>I asked a question a few days ago on how to add paging though records to a web page using an index based solution.</p>
  
  <p>I need to be able to change the sort of  the list of orders using two parameters: strSortBy, and strSortDirection.</p>
  
  <p>I am attempting to do it using IQueryable query:</p>
  
  <pre><code>query = temporders.AsQueryable.OrderBy(strSortBy + " " + strSortDirection)
  </code></pre>
  
  <p>But I am getting an error:</p>
  
  <pre><code>Unable to cast object of type 'System.Linq.EnumerableQuery`1[Order]' to type 'System.Collections.Generic.List`1[Order]'.
  </code></pre>
  
  <p>Line:</p>
  
  <pre><code>orderList.Items = query
  </code></pre>
  
  <p>This is my code for the up(next) button:</p>
  
  <pre><code>Dim temporders As System.Collections.Generic.List(Of Order) = orderList.Items
  
  If Not temporders Is Nothing And Not String.IsNullOrEmpty(strSortBy) Then
      Dim query As IQueryable = Nothing
      query = temporders.AsQueryable.OrderBy(strSortBy + " " + strSortDirection)
      orderList.Items = query
  End If
  </code></pre>
  
  <p>What am I doing wrong in the assignment? Any guidance is appreciated. </p>
  
  <p>Thank you.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I change my code so I apply the OrderBy to the oderList.Items instead that to the tempOrders</p>
  
  <pre><code>orderList.Items.AsQueryable.OrderBy(strSortBy + " " + strSortDirection)
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I was able to make it work this way:</p>
  
  <pre><code>orderList.Items = orderList.Items.AsQueryable.OrderBy(strSortBy + " " + strSortDirection).ToList
  </code></pre>