Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a class that accepts an object as a DataSource. Which is usually a List of Anonymous type. I want to parse this List<t> and create a DataTable using the data in the List<t>. Below is the function in progress to doing this. It accepts an object data, which is the List<t> and so far is able to get the property names to create the columns, but I haven't figured out a way to get the values of the columns. I believe I have to someway cast the object to List<t>, but haven't figured out how to do that yet.

Here is the function in process:

public System.Data.DataTable ToDataTable(object data)
{
   
   System.Data.DataTable table = new System.Data.DataTable();

   System.ComponentModel.PropertyDescriptorCollection props = 
      System.ComponentModel.TypeDescriptor.GetProperties( 
         data.GetType().GetGenericArguments()[0]);
   
   for (int i = 0; i < props.Count; i++)
   {    
      
      System.ComponentModel.PropertyDescriptor prop = props[i];
      
      if (prop.PropertyType.Name.Contains("Nullable"))
         table.Columns.Add(prop.Name);
      else
         table.Columns.Add(prop.Name, prop.PropertyType);
      
   }

   object[] values = new object[props.Count];
   
   foreach( var item in data as System.Collections.IEnumerable)
   {
      
      for( int i = 0; i < values.Lenght; i++; )
         values[i] = props[i].GetValue(item);
      
      table.Rows.Add(values);
      
   }
   
   return (table);
   
}
Posted
Updated 24-Apr-12 11:04am
v5
Comments
Sergey Alexandrovich Kryukov 24-Apr-12 14:38pm    
What exactly do you mean by "a list of anonymous type"? What do you mean by "get the list type"? From where?
First of all, it's good to understand what exactly you are trying to do.
--SA

how about using getproperties instead

C#
Type mytype = data.GetType();

foreach (var prop in mytype.GetProperties())
{
    table.Columns.Add(prop.Name, prop.GetValue(data,null).GetType());
}
 
Share this answer
 
public System.Data.DataTable ToDataTable(object data)
{

System.Data.DataTable table = new System.Data.DataTable();

System.ComponentModel.PropertyDescriptorCollection props =
System.ComponentModel.TypeDescriptor.GetProperties(
data.GetType().GetGenericArguments()[0]);

for (int i = 0; i < props.Count; i++)
{

System.ComponentModel.PropertyDescriptor prop = props[i];

if (prop.PropertyType.Name.Contains("Nullable"))
table.Columns.Add(prop.Name);
else
table.Columns.Add(prop.Name, prop.PropertyType);

}

object[] values = new object[props.Count];

foreach( var item in data as System.Collections.IEnumerable)
{

for( int i = 0; i < values.Lenght; i++; )
values[i] = props[i].GetValue(item);

table.Rows.Add(values);

}

return (table);

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900