Convert IEnumerable to DataTable?

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

Does anyone know how to convert an object of type IEnumerable to a
DataTable?

Thanks,
Ron
 
Ronald S. Cook said:
Does anyone know how to convert an object of type IEnumerable to a
DataTable?

There is no automatic way. You will have to iterate on the contents of the
IEnumerable in a loop, and then add each object returned by the enumerator
to a new DataRow in the DataTable. Of course, this raises the question of
"what kind of table and which columns should it have". If you want to do it
in a general way that will be valid for any IEnumerable, you will have to
use Reflection on the returned objects to find out their properties (or
fields) and then create a datatable with columns of the same type, and then
use Reflection again to get the values and add them to the columns of each
row.

private DataTable ObtainDataTableFromIEnumerable(IEnumerable ien)
{
DataTable dt = new DataTable();
foreach (object obj in ien)
{
Type t = obj.GetType();
PropertyInfo[] pis = t.GetProperties();
if (dt.Columns.Count == 0)
{
foreach (PropertyInfo pi in pis)
{
dt.Columns.Add(pi.Name, pi.PropertyType);
}
}
DataRow dr = dt.NewRow();
foreach (PropertyInfo pi in pis)
{
object value = pi.GetValue(obj, null);
dr[pi.Name] = value;
}
dt.Rows.Add(dr);
}
return dt;
}
 
Thanks. To then update the database, I guess you can't somehow iterate over
the DataTable and recreate the IEnumerable, huh? How then would yoiu update
the database?

Thanks for the help.


Alberto Poblacion said:
Ronald S. Cook said:
Does anyone know how to convert an object of type IEnumerable to a
DataTable?

There is no automatic way. You will have to iterate on the contents of the
IEnumerable in a loop, and then add each object returned by the enumerator
to a new DataRow in the DataTable. Of course, this raises the question of
"what kind of table and which columns should it have". If you want to do
it in a general way that will be valid for any IEnumerable, you will have
to use Reflection on the returned objects to find out their properties (or
fields) and then create a datatable with columns of the same type, and
then use Reflection again to get the values and add them to the columns of
each row.

private DataTable ObtainDataTableFromIEnumerable(IEnumerable ien)
{
DataTable dt = new DataTable();
foreach (object obj in ien)
{
Type t = obj.GetType();
PropertyInfo[] pis = t.GetProperties();
if (dt.Columns.Count == 0)
{
foreach (PropertyInfo pi in pis)
{
dt.Columns.Add(pi.Name, pi.PropertyType);
}
}
DataRow dr = dt.NewRow();
foreach (PropertyInfo pi in pis)
{
object value = pi.GetValue(obj, null);
dr[pi.Name] = value;
}
dt.Rows.Add(dr);
}
return dt;
}
 
Ronald said:
Thanks. To then update the database, I guess you can't somehow iterate
over the DataTable and recreate the IEnumerable, huh? How then would
yoiu update the database?

Thanks for the help.


Alberto Poblacion said:
Ronald S. Cook said:
Does anyone know how to convert an object of type IEnumerable to a
DataTable?

There is no automatic way. You will have to iterate on the contents of
the IEnumerable in a loop, and then add each object returned by the
enumerator to a new DataRow in the DataTable. Of course, this raises
the question of "what kind of table and which columns should it have".
If you want to do it in a general way that will be valid for any
IEnumerable, you will have to use Reflection on the returned objects
to find out their properties (or fields) and then create a datatable
with columns of the same type, and then use Reflection again to get
the values and add them to the columns of each row.

private DataTable ObtainDataTableFromIEnumerable(IEnumerable ien)
{
DataTable dt = new DataTable();
foreach (object obj in ien)
{
Type t = obj.GetType();
PropertyInfo[] pis = t.GetProperties();
if (dt.Columns.Count == 0)
{
foreach (PropertyInfo pi in pis)
{
dt.Columns.Add(pi.Name, pi.PropertyType);
}
}
DataRow dr = dt.NewRow();
foreach (PropertyInfo pi in pis)
{
object value = pi.GetValue(obj, null);
dr[pi.Name] = value;
}
dt.Rows.Add(dr);
}
return dt;
}

Well, an IEnumerable isn't normally updateable in any way and you would
need a lot more data about where the IEnumerable came from before you
can start pushing updates back to the source.

If you need to work with generic IEnumerable objects, then I dare say
this will not be possible, however if you can limit yourself to certain
types of IEnumerable, like LINQ results and similar, then I think you
can get back the original source. Unfortunately I haven't used LINQ to
SQL a lot yet so I don't really know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top