【问题标题】:How to save content of list into OBJECT type如何将列表内容保存为 OBJECT 类型
【发布时间】:2018-02-15 10:40:56
【问题描述】:

我有一个列表/元组,其中填充了一些值。我想将这些值保存到脚本组件中的 OBJECT 类型的变量中。我怎样才能做到这一点。到目前为止,我确实尝试过,但没有成功。

public class ScriptMain : UserComponent
{
    private List<SomeValues> SomeList = new List<SomeValues>();
    public override void PreExecute()
    {
        base.PreExecute();
    }
    public override void PostExecute()
    {
        base.PostExecute();
        //Object type variable in which I am trying to save list
        Variables.LatestDateTime = SomeList;
    }
    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        var MatchingRow = SomeList.FirstOrDefault(d => d.Id == Row.Id);
        if (MatchingRow == null)
        {
            SomeList.Add(new DateTimeValues
            {
                ID = Row.ID,
                LatestDateTime = Row.DateTime
            });
        }
        else
        {
            if (MatchingRow.LatestDateTime < Row.DateTime)
            {
                MatchingRow.LatestDateTime = Row.DateTime;
            }
        }
    }

    public class SomeValues
    {   
        public int Id { get; set; }
        public DateTime LatestDateTime { get; set; }
    }

}

【问题讨论】:

    标签: ssis script-component


    【解决方案1】:

    您需要先将列表写入数据表,然后将数据表设置为变量..

    示例 - 从列表中填充数据表

    List<string> cities = new List<string>();
    cities.Add("New York");
    cities.Add("Mumbai");
    DataTable dt = new DataTable();
    dt.Columns.Add("column1", typeof (string));
    foreach (string str in cities)
    {
        DataRow row = dt.NewRow();
        row["column1"] = str;
        dt.Rows.Add(row);
    }
    

    将您的数据写入您的对象变量

    Dts.Variables["User::NameList"].Value = dt;
    

    然后您可以使用 foreach ado 分子在 SSIS 中循环您的对象变量。

    【讨论】:

    猜你喜欢
    • 2018-11-12
    • 2021-10-04
    • 2015-02-28
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多