【问题标题】:Convert package variable of type System.Object to a List<String> with SSIS C# Script task使用 SSIS C# 脚本任务将 System.Object 类型的包变量转换为 List<String>
【发布时间】:2019-02-08 12:43:21
【问题描述】:
我正在使用以下代码:
Object duplicateDevices = Dts.Variables["User::DuplicateDevices"].Value;
List<DuplicateDeviceModel> lstduplicateDevices = (List<DuplicateDeviceModel>)duplicateDevices;
User::DuplicateDevices 被声明为变量中的对象,但在脚本任务中,同时类型转换我得到类型转换错误;我希望它被转换成List<DuplicateDeviceModel>
【问题讨论】:
标签:
c#
ssis
casting
etl
script-task
【解决方案1】:
尝试使用以下语法转换为IEnumerable:
IEnumerable lstduplicateDevices = duplicateDevices as IEnumerable;
如果有效,您可以使用以下语法指定 `:
IEnumerable<DuplicateDeviceModel> ieDDM = lstduplicateDevices.OfType<DuplicateDeviceModel>();
参考
【解决方案2】:
在脚本中,首先尝试从DuplicateDevices 对象变量填充数据表。在此之后,使用数据表填充类型为 DuplicateDeviceModel 的列表。在不知道您正在使用的列或数据类型的情况下,以下是一个一般示例,列及其数据类型需要根据您正在使用的那些进行调整。
using System.Collections.Generic;
using System.Data.OleDb;
List<DuplicateDeviceModel> lstduplicateDevices = new List<DuplicateDeviceModel>();
DataTable dt = new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.Fill(dt, Dts.Variables["User::DuplicateDevices"].Value);
foreach (DataRow dr in dt.Rows)
{
//convert columns from data type to data types of list as necessary
lstduplicateDevices.Add(new DuplicateDeviceModel() {
ColumnA = Convert.ToInt32(dr[0]),
ColumnB = dr[1].ToString() });
}