I am trying to convert a general collection (List) to a DataTable. I found the following code to help me do this:
// Sorry about indentation public class CollectionHelper { private CollectionHelper() { } // this is the method I have been using public static DataTable ConvertTo<T>(IList<T> list) { DataTable table = CreateTable<T>(); 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<T>() { 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; } }
My problem is that when I change one of the properties of MySimpleClass to a type with a null value, I get the following error:
DataSet does not support System.Nullable<>.
How can I do this using Nullable properties / fields in my class?
collections generics c # nullable datatable
Ronnie Overby Mar 31 '09 at 14:27 2009-03-31 14:27
source share