473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Convert IEnumerable to DataTable?

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

Thanks,
Ron

Mar 20 '08 #1
2 33715
"Ronald S. Cook" <rc***@westinis.comwrote in message
news:E8**********************************@microsof t.com...
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;
}

Mar 21 '08 #2
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" <ea******************************@poblacion.orgwro te
in message news:%2****************@TK2MSFTNGP04.phx.gbl...
"Ronald S. Cook" <rc***@westinis.comwrote in message
news:E8**********************************@microsof t.com...
>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;
}
Mar 22 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Somebody_Out_There | last post by:
Hello, I am trying to create a strongly typed datatable/dataset from an untyped dataset or datatable returned from a stored proc. The structure of the table cannot be static since the results...
1
by: stockblaster | last post by:
Hello all.. Is it possible to convert a DataTable (i create the DataTable from a CSV file) into binary data and save it into an sql 2005 table (into binary field). After that I want to have...
2
by: Bob Weiner | last post by:
This is most likely a trivial question but I'm having a hard time finding an example. I am writing a script using C# 1.1 which iterates over AD accounts and builds a DataTable for those with...
0
by: Sam_Chou | last post by:
How to SqlDataSource become DataTable example: my GridView's datasource is SqlDataSource but i need DataTable (DataSet) so...can SqlDataSource convert to DataTable? thx a lot
3
by: Tom | last post by:
I have a dataTable being returned from my datalayet, I need to convert it to a dataSet so I can do some data manipulation to it prior to populating my datagrid. How can I convert the datatable to a...
9
by: jsoques | last post by:
Hello, I created a Web Service using .Net 2.0 that has a function that returns a DataTable. I can test the function from the web page when I access the .asmx from a browser on localhost and it...
2
by: moondaddy | last post by:
Using c# 3.5, I'm trying to bind a data table to a combo box, but I cant see any data in the list. I'm sure the data table has data. Here's my code: DataSet ds = GetData(); DataTable tb =...
3
by: phanimadhav | last post by:
Hello experts,I have one small doubt i am getting the values from database and filled the datatable.But i don't know how to convert this datatable data to xml.Actually this is my code string...
0
by: =?Utf-8?B?cm9uZSBtYXRpYXM=?= | last post by:
I have the same task to do but everytime I tried to parse my code I get a null value returned after executing "dtMaterials.WriteXml(swMaterials);". I am using the following code: Hope you can hep...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.