To add to my earlier conversation ... this is the code for converting to ToDataset
public static DataSet ToDataSet<T>(this IList<T> list)
{
Type elementType = typeof(T);
DataSet ds = new DataSet();
DataTable t = new DataTable();
ds.Tables.Add(t);
//add a column to table for each public property on T
foreach (var propInfo in elementType.GetProperties())
{
t.Columns.Add(propInfo.Name, propInfo.PropertyType);
}
//go through each property on T and add each value to the table
foreach (T item in list)
{
DataRow row = t.NewRow(); foreach (var propInfo in elementType.GetProperties())
{
row[propInfo.Name] = propInfo.GetValue(item, null);
}
t.Rows.Add(row);
}
return ds;
}
So now, do I need to change this definition too. Cos, this takes IList as its parameter.
Satish Chilkury
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
SATISD9X
Contributor
2713 Points
487 Posts
Re: Convert ComplexObject to IList
Apr 27, 2012 01:03 PM|LINK
Greetings ... Thanks for your reply.
To add to my earlier conversation ... this is the code for converting to ToDataset
public static DataSet ToDataSet<T>(this IList<T> list) { Type elementType = typeof(T); DataSet ds = new DataSet(); DataTable t = new DataTable(); ds.Tables.Add(t); //add a column to table for each public property on T foreach (var propInfo in elementType.GetProperties()) { t.Columns.Add(propInfo.Name, propInfo.PropertyType); } //go through each property on T and add each value to the table foreach (T item in list) { DataRow row = t.NewRow(); foreach (var propInfo in elementType.GetProperties()) { row[propInfo.Name] = propInfo.GetValue(item, null); } t.Rows.Add(row); } return ds; }So now, do I need to change this definition too. Cos, this takes IList as its parameter.
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
~ Please Mark as Answer if it solves your query ~