I am working in C# and .net core. I have an object which consist of multiple lists of strings and i want to convert this object into datatable.
I have tried this code, but it failed:
public static DataTable ObjectToData(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, typeof(string));
dt.Rows[0][f.Name] = f.GetValue(o, null);
}
catch { }
});
return dt;
}