Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following approach uses a <code>DataTable</code> list to address a dynamic number of datatables. The code produces a <code>List</code> of <code>IEnumerable&lt;object&gt;</code>. This example assumes that the column value that will be added to the final item structure for each other datatable in the join lies in the same position (in our case I used <code>row2[1]</code>, so I take the second position).</p> <p><strong>[EDIT]</strong> I also added an example of joining more than one column / joined table</p> <pre><code> // create the collection List&lt;DataTable&gt; dts = new List&lt;DataTable&gt;(); // add some test datatables for (int i = 0; i &lt; 10; i++) { var dt = new DataTable(); dt.TableName = i.ToString(); dt.Columns.Add("ID"); dt.Columns.Add("col" + i.ToString()); dt.Columns.Add("otherCol" + i.ToString()); dt.Rows.Add(1, "x1" + i.ToString(), DateTime.Now); dt.Rows.Add(2, "x2" + i.ToString(), DateTime.Now); dts.Add(dt); } // get the ID column position in the first table var idPosition = dts[0].Columns["ID"].Ordinal; // used for storing the results var results = new List&lt;IEnumerable&lt;object&gt;&gt;(); // add the columns from the first table results = dts[0].AsEnumerable() .Select(j =&gt; j.ItemArray.AsEnumerable()).ToList(); // join all tables dts.Skip(1).ToList().ForEach((list) =&gt; { results = results .AsEnumerable() .Join( list.AsEnumerable(), x =&gt; x.Skip(idPosition).First(), x =&gt; x["ID"], // select the second column (row1, row2) =&gt; row1.Concat(new[] { row2[1] })) // replace the preceding line with // the following one to select the second and the third column //(row1, row2) =&gt; row1.Concat(new[] { row2[1], row2[2] })) .ToList(); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload