I found this code snippet that allows for you to use enumerable extension on a list passed into the bulk copy.
public static DataTable AsDataTable<T>(this IEnumerable<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
var table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
I have limited knowledge on the matters but I know this can convert lists to data tables without having to manually map it all out and allows for you to do something like:
bulkCopy.WriteToServer(companies.AsDataTable());
My problem is, I have nested objects in the companies object list and I don't believe the above snippet will allow me to read the nested objects into the data table as expected. Would the above snippet still read nested objects or what modifications do I need for it to do so?
Thanks!
Aucun commentaire:
Enregistrer un commentaire