title: How to convert LINQ ToList() function to datatable c#
body: <p>this is how my xml look like</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Root&gt;
    &lt;orders&gt;
      &lt;OrderID&gt;10248&lt;/OrderID&gt;
      &lt;CustomerID&gt;VINET&lt;/CustomerID&gt;
      &lt;EmployeeID&gt;5&lt;/EmployeeID&gt;
      &lt;OrderDate&gt;1996-07-04T00:00:00&lt;/OrderDate&gt;
      &lt;RequiredDate&gt;1996-08-01T00:00:00&lt;/RequiredDate&gt;
      &lt;ShippedDate&gt;1996-07-16T00:00:00&lt;/ShippedDate&gt;
      &lt;ShipVia&gt;3&lt;/ShipVia&gt;
      &lt;Freight&gt;32.3800&lt;/Freight&gt;
      &lt;ShipName&gt;Vins et alcools Chevalier&lt;/ShipName&gt;
      &lt;ShipAddress&gt;59 rue de l'Abbaye&lt;/ShipAddress&gt;
      &lt;ShipCity&gt;Reims&lt;/ShipCity&gt;
      &lt;ShipPostalCode&gt;51100&lt;/ShipPostalCode&gt;
      &lt;ShipCountry&gt;France&lt;/ShipCountry&gt;
    &lt;/orders&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>i parse the above xml using LINQ this way</p>
  
  <pre><code>document.Descendants("orders").Select(c =&gt; c).ToList()
  </code></pre>
  
  <p>i want to list xml data in tabular format just like sql query return data.
  i search and got a link <code>http://stackoverflow.com/questions/18608959/convert-linq-query-results-to-datatable-c-sharp</code> the link suggested to use <code>CopyToDataTable() extension</code> method which i followed but still i am not getting result in tabular format.</p>
  
  <p>so guide me how could i convert my <code>ToList() to Datatable</code> as a result output would look like</p>
  
  <pre><code>OrderID      CustomerID      EmployeeID
  -------      ----------      -----------
  10248        VINET           5
  10249        AJAY            11
  11027        Smith           09
  </code></pre>
  
  <h2>here is my full code for CopyToDataTable which i got from MSDN</h2>
  
  <pre><code>public static class DataSetLinqOperators
  {
      public static DataTable CopyToDataTable&lt;T&gt;(this IEnumerable&lt;T&gt; source)
      {
          return new ObjectShredder&lt;T&gt;().Shred(source, null, null);
      }
  
      public static DataTable CopyToDataTable&lt;T&gt;(this IEnumerable&lt;T&gt; source,
                                                  DataTable table, LoadOption? options)
      {
          return new ObjectShredder&lt;T&gt;().Shred(source, table, options);
      }
  
  }
  
  public class ObjectShredder&lt;T&gt;
  {
      private FieldInfo[] _fi;
      private PropertyInfo[] _pi;
      private Dictionary&lt;string, int&gt; _ordinalMap;
      private Type _type;
  
      public ObjectShredder()
      {
          _type = typeof(T);
          _fi = _type.GetFields();
          _pi = _type.GetProperties();
          _ordinalMap = new Dictionary&lt;string, int&gt;();
      }
  
      public DataTable Shred(IEnumerable&lt;T&gt; source, DataTable table, LoadOption? options)
      {
          if (typeof(T).IsPrimitive)
          {
              return ShredPrimitive(source, table, options);
          }
  
  
          if (table == null)
          {
              table = new DataTable(typeof(T).Name);
          }
  
          // now see if need to extend datatable base on the type T + build ordinal map
          table = ExtendTable(table, typeof(T));
  
          table.BeginLoadData();
          using (IEnumerator&lt;T&gt; e = source.GetEnumerator())
          {
              while (e.MoveNext())
              {
                  if (options != null)
                  {
                      table.LoadDataRow(ShredObject(table, e.Current), (LoadOption)options);
                  }
                  else
                  {
                      table.LoadDataRow(ShredObject(table, e.Current), true);
                  }
              }
          }
          table.EndLoadData();
          return table;
      }
  
      public DataTable ShredPrimitive(IEnumerable&lt;T&gt; source, DataTable table, LoadOption? options)
      {
          if (table == null)
          {
              table = new DataTable(typeof(T).Name);
          }
  
          if (!table.Columns.Contains("Value"))
          {
              table.Columns.Add("Value", typeof(T));
          }
  
          table.BeginLoadData();
          using (IEnumerator&lt;T&gt; e = source.GetEnumerator())
          {
              Object[] values = new object[table.Columns.Count];
              while (e.MoveNext())
              {
                  values[table.Columns["Value"].Ordinal] = e.Current;
  
                  if (options != null)
                  {
                      table.LoadDataRow(values, (LoadOption)options);
                  }
                  else
                  {
                      table.LoadDataRow(values, true);
                  }
              }
          }
          table.EndLoadData();
          return table;
      }
  
      public DataTable ExtendTable(DataTable table, Type type)
      {
          // value is type derived from T, may need to extend table.
          foreach (FieldInfo f in type.GetFields())
          {
              if (!_ordinalMap.ContainsKey(f.Name))
              {
                  DataColumn dc = table.Columns.Contains(f.Name) ? table.Columns[f.Name]
                      : table.Columns.Add(f.Name, f.FieldType);
                  _ordinalMap.Add(f.Name, dc.Ordinal);
              }
          }
          foreach (PropertyInfo p in type.GetProperties())
          {
              if (!_ordinalMap.ContainsKey(p.Name))
              {
                  DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name]
                      : table.Columns.Add(p.Name, p.PropertyType);
                  _ordinalMap.Add(p.Name, dc.Ordinal);
              }
          }
          return table;
      }
  
      public object[] ShredObject(DataTable table, T instance)
      {
  
          FieldInfo[] fi = _fi;
          PropertyInfo[] pi = _pi;
  
          if (instance.GetType() != typeof(T))
          {
              ExtendTable(table, instance.GetType());
              fi = instance.GetType().GetFields();
              pi = instance.GetType().GetProperties();
          }
  
          Object[] values = new object[table.Columns.Count];
          foreach (FieldInfo f in fi)
          {
              values[_ordinalMap[f.Name]] = f.GetValue(instance);
          }
  
          foreach (PropertyInfo p in pi)
          {
              values[_ordinalMap[p.Name]] = p.GetValue(instance, null);
          }
          return values;
      }
  }
  </code></pre>
  
  <p>and then call this way <code>DataTable dtt =document.Descendants("orders").Select(c =&gt; c).ToList().CopyToDataTable();</code> but it did not worked the way i want the output.</p>