473,609 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 33750
"Ronald S. Cook" <rc***@westinis .comwrote in message
news:E8******** *************** ***********@mic rosoft.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 ObtainDataTable FromIEnumerable (IEnumerable ien)
{
DataTable dt = new DataTable();
foreach (object obj in ien)
{
Type t = obj.GetType();
PropertyInfo[] pis = t.GetProperties ();
if (dt.Columns.Cou nt == 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.o rgwrote
in message news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
"Ronald S. Cook" <rc***@westinis .comwrote in message
news:E8******** *************** ***********@mic rosoft.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 ObtainDataTable FromIEnumerable (IEnumerable ien)
{
DataTable dt = new DataTable();
foreach (object obj in ien)
{
Type t = obj.GetType();
PropertyInfo[] pis = t.GetProperties ();
if (dt.Columns.Cou nt == 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
6814
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 are dynamic which means that I am right now creating the schema/structure of the datatable from a datareader using reader.GetSchemaTable() and then looping thru the reader to populate values. Is there any other efficient way of achieving this?...
1
2039
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 the ability to add a row to the beginning of the to the binary data.. And ability to remove a row..
2
12343
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 passwords expiring within various periods of time. After sending mail to the users, I want to send a report to the help desk with a table of users included. It seems to me that the easiest way to do it is to convert the DataTable to HTML. ...
0
3127
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
1971
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 dataset?
9
12011
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 works. I can also test the function using VB6 and the xmlhttp activex object. The problem I have now is when using VS 2005 or VB.Net 2005 Express and creating a web references is that the proxy created doesn't map the function as returning a DataTable...
2
14196
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 = ds.Tables; myCombo.DataContext = tb; myCombo.DisplayMemberPath = tb.Columns.ToString(); myCombo.SelectedValuePath = tb.Columns.ToString(); any idea what I'm doing wrong?
3
1198
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 query = "SELECT TOP 10 * FROM Details"; SqlConnection cn = new SqlConnection(connectionstring); cn.Open(); SqlDataAdapter adpt = new SqlDataAdapter(new SqlCommand(query, cn)); System.Data.DataTable dt = new...
0
2862
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 me out with this. Thanks. DataTable dtMaterials = new DataTable(); StringWriter swMaterials = new StringWriter(); swMaterials = null; string strMaterials = string.Empty;
0
8133
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8083
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8224
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8406
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7013
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4026
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2535
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.