I want to convert a List to a Datatable. I am using the following code :
public static DataTable ConvertToDataTable(object o)
{
DataTable dt = new DataTable("OutputData");
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
o.GetType().GetProperties().ToList().ForEach(f =>
{
try
{
f.GetValue(o, null);
dt.Columns.Add(f.Name, f.PropertyType);
dt.Rows[0][f.Name] = f.GetValue(o, null);
}
catch { }
});
return dt;
}
The following is the class object that I am trying to pass to the ConverToDataTable method :
public class Class1
{
private List<int> _intList;
public List<int> intList
{
get
{
return _intList;
}
set
{
_intList = value;
}
}
}
But when I am trying to convert the class1 object to DataTable I am getting the datatable columns as count and Capacity.. What am I doing wrong?