title: How to convert DataTable to List<object>?
body: <p>I have a strongly typed class <code>PersonExport</code>.  I initially get data into a <code>DataTable</code> and call the following method on the <code>DataTable</code> to convert it to <code>List&lt;PersonExport&gt;</code>:</p>
  
  <pre><code>public static List&lt;T&gt; ConvertToList&lt;T&gt;(DataTable dt, out string message)
      {
          message = string.Empty;
          var list = new List&lt;T&gt;();
  
  
          try
          {
              var columnNames = dt.Columns.Cast&lt;DataColumn&gt;()
              .Select(c =&gt; c.ColumnName)
              .ToList();
  
              var properties = typeof(T).GetProperties();
  
              list = dt.AsEnumerable().Select(row =&gt;
              {
                  var objT = Activator.CreateInstance&lt;T&gt;();
  
                  foreach (var pro in properties)
                  {
                      if (columnNames.Contains(pro.Name))
                      {
                          var value = row[pro.Name];
                          var typeName = value.GetType().FullName;
  
                          if (typeName == "MySql.Data.Types.MySqlDateTime")
                          {
  
                              var mySqlDateTime = (MySqlDateTime) value;
                              if (mySqlDateTime.IsValidDateTime)
                              {
                                  value = Convert.ToDateTime(mySqlDateTime.ToString());
                                  pro.SetValue(objT, value, null);
                              }
                          }
                          else
                          {
                              pro.SetValue(objT, row.IsNull(pro.Name) ? null : value, null);
                          }
                      }
                  }
  
                  return objT;
              }).ToList();
  
          }
          catch (Exception ex)
          {
              message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
          }
  
          return list;
      }
  </code></pre>
  
  <p>However, once I start removing columns from the <code>DataTable</code> returned, it no longer works because the columns in the <code>DataTable</code> don't match up with the properties in the <code>PersonExport</code> list.</p>
  
  <p>I am eventually using the exported list here to export to excel, but it is not working since I have modified by <code>DataTable</code> and it can't Deserialize into a <code>List&lt;PersonExport&gt;</code>:</p>
  
  <pre><code>//Trying to get data into List&lt;object&gt;
  List&lt;object&gt; persons = GetPersonExport(query, out message);
  var exportData = new Dictionary&lt;string, List&lt;object&gt;&gt; { { "xldata", persons} };
  
  //Deserialize to List&lt;object&gt; to export
  var persons = JsonConvert.DeserializeObject&lt;List&lt;object&gt;&gt;(args["xldata"]);
  </code></pre>
  
  <p>The above line just returns a List of empty objects.</p>
  
  <p>A few things got me thinking, but I am wondering what might be the best approach.  I am using the EPPLUS library to export data to excel and it has the option of hiding columns, so would it be better to just export the whole object and hide columns you don't want, this way you avoid the anonymous type or what I can do is still get the whole object, but then convert it to a DataTable and then remove the columns? Thoughts?</p>