title: .NET - Convert Generic Collection to DataTable
body: <p>I am trying to convert a generic collection (List) to a DataTable. I found the following code to help me do this:</p>
  
  <pre><code>// Sorry about indentation
  public class CollectionHelper
  {
  private CollectionHelper()
  {
  }
  
  // this is the method I have been using
  public static DataTable ConvertTo&lt;T&gt;(IList&lt;T&gt; list)
  {
      DataTable table = CreateTable&lt;T&gt;();
      Type entityType = typeof(T);
      PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
  
      foreach (T item in list)
      {
          DataRow row = table.NewRow();
  
          foreach (PropertyDescriptor prop in properties)
          {
              row[prop.Name] = prop.GetValue(item);
          }
  
          table.Rows.Add(row);
      }
  
      return table;
  }    
  
  public static DataTable CreateTable&lt;T&gt;()
  {
      Type entityType = typeof(T);
      DataTable table = new DataTable(entityType.Name);
      PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
  
      foreach (PropertyDescriptor prop in properties)
      {
          // HERE IS WHERE THE ERROR IS THROWN FOR NULLABLE TYPES
          table.Columns.Add(prop.Name, prop.PropertyType);
      }
  
      return table;
  }
  }
  </code></pre>
  
  <p>My problem is that when I change one of the properties of MySimpleClass to a nullable type, I get the following error:</p>
  
  <p><code>DataSet does not support System.Nullable&lt;&gt;.</code></p>
  
  <p>How can I do this with Nullable properties/fields in my class?</p>