我通过创建一个anonymous object
并用我们需要的值填充它来解决这个问题。通过这种方式,您可以展平您的对象并仅使用您需要的属性。
var newObjects = customer.Records.Select(record => new
{
customer.Id,
record.DisplayName,
record.TotalCount,
record.Conditions.IsActive,
record.Conditions.SusperCondition.IsSuper,
Status = (int)record.Status
});
然后只需将这个新对象传递给ObjectReader
using (var reader = ObjectReader.Create(newObjects))
{
DataAccess.BulkInsert(serverName, databaseName, tableName, reader, SetBulkCopyMappings);
}
批量插入 -
internal static void BulkInsert(string serverName, string databaseName, string tableName, IDataReader dataReader, Action<SqlBulkCopy> setMappings)
{
using (var bulkCopy = new SqlBulkCopy(BuildConnectionString(serverName, databaseName)))
{
bulkCopy.DestinationTableName = tableName;
setMappings(bulkCopy);
bulkCopy.WriteToServer(dataReader);
}
}