title: Dynamic LINQ to join on dynamic columns on DataTable
body: <p>I have a situation that I am currently unsure of how to proceed.</p>
  
  <p>I have two datatables that are populated from a database, I also have a list of column names available that can be used to join these two datatables together. I wish to write a set of LINQ queries that will: </p>
  
  <ol>
  <li>Show rows from both datatables (inner join, used for updating one from another).</li>
  <li>Show rows from one datatable that don't exist in the other (one query, left join used for inserts, the other a right join used for deletes).</li>
  </ol>
  
  <p>Now I know how to do this with normal LINQ to objects or datatables, however in this case I need to apply the columns to join on dynamically, and there could be more than one. Looking at the following partial example code:</p>
  
  <pre><code>table1.AsEnumerable()
  .Join(table2.AsEnumerable(),
      dr1 =&gt; dr1.Field&lt;string&gt;("ID"),
      dr2 =&gt; dr2.Field&lt;string&gt;("ID"),
      (dr1, dr2) =&gt; new
      {
          FieldID = dr1.Field&lt;string&gt;("ID"),
          CdGroup = dr2.Field&lt;string&gt;("Name")
      })
  </code></pre>
  
  <p>The issues are that I don't know the field type so the <code>.Field&lt;string&gt;</code> parts of the statement can't be applied. Also if their are multiple join columns, then I will need to have multiple join statements.</p>
  
  <p>I have read up on dynamic LINQ and it seems quite promising, however I haven't managed to find any information on dynamic LINQ joins like I am trying to do. I know I could probably get the same results using nested loops or the<code>.Select()</code> method on the datatable, but I am trying to apply LINQ to some of the tougher queries that I require.</p>
  
  <p>Do anyone have any pointers or examples of how I could achieve this, or should I just revert to using a non-LINQ approach?</p>
  
  <p>Thanks very much.</p>