title: Linq to datatable with unknown columns
body: <p>I am having SQL query like this (will be parametrized once I make this work):</p>
  <pre><code>using (FbCommand cmd = new FbCommand(&quot;SELECT MAGACINID, ROBAID, SUM(KOLICINA) AS GOD&quot; + godina.ToString() + &quot; FROM STAVKA WHERE VRDOK = 13 OR VRDOK = 15 GROUP BY MAGACINID, ROBAID ORDER BY ROBAID&quot;, con))
                          {
                              using (FbDataAdapter da = new FbDataAdapter(cmd))
                              {
                                  DataTable tempDT = new DataTable();
                                  if (dt.Rows.Count == 0)
                                  {
                                      da.Fill(dt);
                                      continue;
                                  }
  
                                  da.Fill(tempDT);
  
                                  var result = dt.AsEnumerable()
                                      .Join(tempDT.AsEnumerable(),
                                      x =&gt; new { field1 = x[&quot;MAGACINID&quot;], field2 = x[&quot;ROBAID&quot;] },
                                      y =&gt; new { field1 = y[&quot;MAGACINID&quot;], field2 = y[&quot;ROBAID&quot;] },
                                      (x, y) =&gt; new {
                                          x,
                                          addYear = y[&quot;GOD&quot; + godina.ToString()]
                                      });
  
                                  dt = LINQResultToDataTable(result);
                              }
                          }
  </code></pre>
  <p>And here is <code>LINQResultToDataTable()</code> method</p>
  <pre><code>
          private DataTable LINQResultToDataTable&lt;T&gt;(IEnumerable&lt;T&gt; Linqlist)
          {
              DataTable dt = new DataTable();
              PropertyInfo[] allColumns = null;
  
              if (Linqlist == null) return dt;
  
              foreach (T Record in Linqlist)
              {
                  if (allColumns == null)
                  {
                      allColumns = ((Type)Record.GetType()).GetProperties();
  
                      // Record at position 0 is whole object and needs to be broke into pieces
                      // Record at position 1 is new column
  
                      foreach (PropertyInfo GetProperty in allColumns)
                      {
                          Type colType = GetProperty.PropertyType;
  
                          if ((colType.IsGenericType) &amp;&amp; (colType.GetGenericTypeDefinition()
                                 == typeof(Nullable&lt;&gt;)))
                          {
                              colType = colType.GetGenericArguments()[0];
                          }
  
                          dt.Columns.Add(new DataColumn(GetProperty.Name, colType));
                      }
                  }
  
                  DataRow dr = dt.NewRow();
  
                  foreach (PropertyInfo pinfo in allColumns)
                  {
                      dr[pinfo.Name] = pinfo.GetValue(Record, null) == null ? DBNull.Value : pinfo.GetValue
                             (Record, null);
                  }
  
                  dt.Rows.Add(dr);
              }
              return dt;
          }
  </code></pre>
  <p>First datatable fill is okay and it fills it with these columns:</p>
  <pre><code>MagacinID
  RobaID
  God2021
  </code></pre>
  <p>Now when I try to join new table using linq I should only add <code>God2020</code> as new column.
  Problem is that when I am creating output of linq I cannot tell it to break <code>x</code> object into columns but I need to take it whole so linq result looks like this:</p>
  <pre><code>x (this one have ItemArray with all columns)
  God2020
  </code></pre>
  <p>And new datatable looks same.</p>
  <p>What I tried doing is inside <code>LINQResultToDataTable</code> method to take <code>0th</code> element of <code>Result</code> and break it into columns, add to datatable, add new one (1st element) and populate it (see comment in that method) but I am stuck and do not know how to do that. Anyone have any solution?</p>
  <p>Edit: I made it not clear so here is more explanation.</p>
  <p>I have <code>foreach</code> loop which loops through <code>5 databases</code> and inside that foreach loop is this code.</p>
  <p>First database returns these columns</p>
  <pre><code>MagacinID
  RobaID
  God2021
  </code></pre>
  <p>Second</p>
  <pre><code>MagacinID
  RobaID
  God2020
  </code></pre>
  <p>Third</p>
  <pre><code>MagacinID
  RobaID
  God2019
  </code></pre>
  <p>Fourth</p>
  <pre><code>MagacinID
  RobaID
  God2018
  </code></pre>
  <p>Fifth</p>
  <pre><code>MagacinID
  RobaID
  God2017
  </code></pre>
  <p>I want all this merged into one datatable on columns <code>MagacinID AND RobaID</code> like this:</p>
  <pre><code>MagacinID
  RobaID
  God2021
  God2020
  God2019
  God2018
  God2017
  </code></pre>