i have a number of Tables setup in SQl Server
i have transferred all of these into a dbml file for normal LinqToSql Use.
i’m wondering if it is possible to alter the class in code to only select the Columns of the table and not the Linked tables
i.e. if i wanted to pass the class to a method without the linked table properties how would i go about doing it
more information essentially i’m trying to use an extension i have written to use SQLbulkCopy instead of context.SubmitChanges but run into mapping problems due to extra properties
I realise I could use anonymous types but thought that would overwrite any benefit of the Extension methods.
the other option is to alter the ToDataTable extension.
public static void SqlBulkInsert<T>(this IEnumerable<T> source, string connectionString, string tableName)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
var bulkCopy = new SqlBulkCopy(conn) {DestinationTableName = tableName};
conn.Open();
var item = source.ToDataTable();
bulkCopy.WriteToServer(item);
conn.Close();
}
}
public static DataTable ToDataTable<T>(this IEnumerable<T> source)
{
using (var dt = new DataTable())
{
var toList = source.ToList();
for (var index = 0; index < typeof(T).GetProperties().Length; index++)
{
var info = typeof(T).GetProperties()[index];
dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
for (var index = 0; index < toList.Count; index++)
{
var t = toList[index];
var row = dt.NewRow();
foreach (var info in typeof(T).GetProperties())
{
row[info.Name] = info.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}
}
Right so i solved it
Usage Example
i have had to define a static List and only process those property types contained within the list
a collegue suggested using the isclass attribute on propertyType but this had issues
of course
i would be happy for some critique if there are any other methods or something i may have missed
hope that this helps someone!
i.e. like this