title: Combining DataTables obtained from DataSet.ReadXML
body: <p>I have a DataGridView that I would like to populate with data read from an XML file which I read from a network folder. These XML files have different schemas, the most complex of which resembles the following structure.   </p>
  
  <blockquote>
    <p>Orders (Attribute 1)</p>
    
    <blockquote>
      <p>Order (Attribute 2) (Attribute 3)</p>
      
      <blockquote>
        <p>Line</p>
        
        <blockquote>
          <p>Line Number
          Part Number
          Serial Number</p>
        </blockquote>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p>This data is read into memory through a combination of FileInfo.OpenText, XmlReader.Create, and DataSet.ReadXML. When DataSet.ReadXML executes, I am returned a DataSet object (named "xmlContents") with three DataTables: Orders, Order and Line. I wish to bind this data to a single DataGridView in a Windows Form application I am creating.</p>
  
  <p>When I tried merging the data tables together, using the below methodology, I get a Null Reference Exception.</p>
  
  <pre><code>foreach (DataTable dt in xmlContents.Tables)
              {
                  if (onFirstTable)
                  {
                      mergedTable = dt.Clone();
                      onFirstTable = false;
                  }
                  else
                  {
                      mergedTable.Merge(dt);
                  }
              }
  </code></pre>
  
  <p>I have checked, and there ARE DataRelations between the three different tables.   However, the columns are system-generated.   Is there a way, perhaps using LINQ, that I can return a DataSet that would combine the data from these three tables into a single DataTable?   I would like to do this in a generic manner, without having to write a LINQ such as </p>
  
  <p><code>from x join y join z 
  select x.Attribute1, y.Attribute2, y.Attribute3, z.LineNumber, z.PartNumber, z.SerialNumber</code> </p>
  
  <p>as different files have the different schemas, and I do not know the names of the system-generated primary keys at build-time.</p>
  
  <hr>
  
  <p>Edit: I have implemented a LINQ query, after determining what the ystem-generated PKs were for my tables.   </p>
  
  <pre><code>IEnumerable&lt;DataRow&gt; query = 
                  (from rpos in xmlContents.Tables[0].AsEnumerable()
                  join rpo in xmlContents.Tables[1].AsEnumerable()
                      on rpos.Field&lt;object&gt;("RPOs_Id") equals rpo.Field&lt;object&gt;("RPOs_Id")
                  join line in xmlContents.Tables[2].AsEnumerable()
                      on rpo.Field&lt;object&gt;("RPO_Id") equals line.Field&lt;object&gt;("RPO_Id")
                  select new { rpos, rpo, line }).Cast&lt;DataRow&gt;();
  </code></pre>
  
  <p>However, when running the application I get the following error:</p>
  
  <blockquote>
    <p>Unable to cast object of type '&lt;>f__AnonymousType1`3[System.Data.DataRow,System.Data.DataRow,System.Data.DataRow]' to type 'System.Data.DataRow'.</p>
  </blockquote>