ibatis-user-cs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: Creating a DataTable from an IList returned from the data mapper
Date Sat, 15 Jul 2006 22:43:39 GMT
What ASP.Net components only bind to DataTables and DataSets?

I think its ineffecient to get an IList back of items then iterate
through it a second time to convert it to a DataTable. I'm in favor of
an implementation that creates the DataTable from the IDataReader
directly.

--- Riccardo d'Errico <riccardo.derrico@gmail.com> wrote:

> It arrived to me to have to convert back to a DataTable the IList
> result I
> get from the DataMapper instance.
> This is mainly because some asp.net components accepts binding only
> to
> dataset or datatable
> Here is the code I use if somebody has the same problem.
> This code only works in .Net 2.0
> public static DataTable GetDataTableFromIList<T>(List<T> aIList)
> {
> 
> DataTable _returnTable = new DataTable();
> 
> if (aIList.Count>0)
> 
> {
> 
> //Creates the table structure looping in the in the first element of
> the
> list
> 
> object _baseObj = aIList[0];
> 
> Type objectType = _baseObj.GetType();
> 
> PropertyInfo[] properties = objectType.GetProperties();
> 
> DataColumn _col;
> 
> foreach (PropertyInfo property in properties)
> 
> {
> 
> _col = new DataColumn();
> 
> _col.ColumnName = (string)property.Name;
> 
> _col.DataType = property.PropertyType;
> 
> _returnTable.Columns.Add(_col);
> 
> }
> 
> //Adds the rows to the table
> 
> DataRow _row;
> 
> foreach (object objItem in aIList)
> 
> {
> 
> _row = _returnTable.NewRow();
> 
> foreach (PropertyInfo property in properties)
> 
> {
> 
> _row[property.Name] = property.GetValue(objItem, null);
> 
> }
> 
> _returnTable.Rows.Add(_row);
> 
> }
> 
> }
> 
> return _returnTable;
> 
> }
> 


Mime
View raw message