title: Dynamic Linq query - how do I build the select clause?
body: <p>I'm trying to do some tests with Dynamic Linq at the moment, but being new at it, I am having troubles.</p>
  
  <p>Currently I have one DataTable object that I have filled by an SQL query to a database. Now I want to execute dynamic linq queries on this DataTable. This may seem illogical, but my end goal is be able to perform joins on two different DataTables that are filled by two different databases, so with that in mind I hope it makes more sense.
  I'm trying to understand a simpler situation and experiment a bit with it before I go on to solve that main problem.</p>
  
  <p>Question one is that I am not entirely sure about the choice between IQueryable(Of T) and IEnumerable(Of T). The way I understand is that if you do everything in-memory you choose IEnumerable. I would think that fits for my case then, right? However, when I looked at changing the IEnumerable in IQuerbyable (in the code snippet below), I was surprised to see that "x.Item(Fieldname)" does not work!? What am I missing?  The error it gave is: "Late Binding operations cannot be converted to an Expression Tree".</p>
  
  <p>Anyway, lets take a look. I already got part of it working:</p>
  
  <pre><code>    Dim desc As String = "Description"
      Dim status As String = "Status"
  
      Dim query As IEnumerable(Of DataRow) = From x In tab.AsEnumerable()
      query = query.Where(Function(x As Object) x.Item(status) &lt; 100)
  
      For Each row As DataRow In query.ToList()
      'Do something
      Next row
  </code></pre>
  
  <p>This seems to work fine. I used the two string variables, because in the end I want to dynamically decide which field to take. So, "x!Description" is not the way to go for me. However, now I wanted to add a projection to the query, i.e. Select a number of columns.My expectation was that this should work like this:</p>
  
  <pre><code>query = query.Select(Of String)(Function(x As Object) x.Item(desc))
  </code></pre>
  
  <p>(N.B.: I use Select(Of String), because this specific column has that as type. This should of course in the end be dynamically filled too)</p>
  
  <p>However, this fails during runtime on an InvalidCastException: "Unable to cast object of type 'WhereSelectEnumerableIterator<code>2[System.Data.DataRow,System.String]' to type 'System.Collections.Generic.IEnumerable</code>1[System.Data.DataRow]'." I dont really understand what happens here; I'm guessing something is wrong because I want to return DataRow's, but at the moment only select one field, which happens to be a String.  Can anyone explain/help me?</p>
  
  <p>Thanks in advance!</p>
  
  <p>OK, EDIT, as I should probably have given some more info from scratch: 
  I could have a variable number of where- or select-clauses. My idea was I could add these dynamically by looping through the list that contains this information. So I thought I kinda need the .Where() and .Select() functions that IEnumerable offers.</p>