Linq Lambda表达式转换为SSIS

时间:2015-07-06 08:06:16

标签: c# linq lambda ssis

我有一些现有的代码需要转换为在ssis c#中读取。

string conRef = ""; 
manOrder = new List<ManOrder>(); 
order = manOrder.Where(w => w.Ref == vRef).ToList();

我相信这会使用mvc(现有代码)中的模型创建一个列表。

如何使用c#在ssis脚本任务中创建此功能。

我在manOrder的新班级中创建了属性。

基本上我需要代码从数据库中构建一个xml数据(目前使用硬编码值来测试理论。)

尝试将此代码放入ssis脚本任务时出错:

public void Main()
{
    string namespace = @"x-schema:http://10.0.0.2/THst%20Web%20Service/Schema/Calls1_2.dtd";
    string vRef = "";
    bool naccount = false;
    List<Order> manOrder;
    int orderCount = 0;
    using (MemoryStream memStream = new MemoryStream())
    {
        XmlTextWriter xmlWrt = null;
        System.Xml.XmlDocument iDocument = new System.Xml.XmlDocument();
        xmlWrt = new XmlTextWriter(memStream, System.Text.Encoding.UTF8);

        ReadFromDatabase rd = new ReadFromDatabase();
        List<Man> manData = new List<Man>();
        List<ManOrder> manOrder = new List<ManOrder>();
        rd.GetManData(out manData, out manOrder);

        //Start the Document
        xmlWrt.WriteStartDocument();
        xmlWrt.WriteStartElement("", "Calls", namespace);

        foreach (Man man in manData)
        {
            vRef = man.conRef;

            xmlWrt.WriteStartElement("", "Call", namespace);
            xmlWrt.WriteElementString("CallRef", namespace, "");
            xmlWrt.WriteStartElement("", "Details", namespace);
            xmlWrt.WriteElementString("Condition", namespace, "EDITABLE"); //Database to be fed in here
            xmlWrt.WriteElementString("CallType", namespace, "COM"); //Database to be fed in here
            xmlWrt.WriteElementString("Select0", namespace, "D"); //Database to be fed in here
            xmlWrt.WriteElementString("Select1", namespace, ""); //Database to be fed in here
            xmlWrt.WriteElementString("Select2", namespace, ""); //Database to be fed in here
            xmlWrt.WriteElementString("Select3", namespace, ""); //Database to be fed in here
            xmlWrt.WriteEndElement(); //Details

            xmlWrt.WriteStartElement("", "Defaults", namespace);
            //Order Date Time
            xmlWrt.WriteStartElement("", "Default", namespace);
            xmlWrt.WriteElementString("Label", namespace, "ORDERTIME");
            xmlWrt.WriteElementString("Value", namespace, ""); 
            xmlWrt.WriteElementString("Iteration", namespace, "0");
            xmlWrt.WriteEndElement(); //Default

            //Warehouse
            xmlWrt.WriteStartElement("", "Default", namespace);
            xmlWrt.WriteElementString("Label", namespace, "WARE");
            xmlWrt.WriteElementString("Value", namespace, ""); 
            xmlWrt.WriteElementString("Iteration", namespace, "0");
            xmlWrt.WriteEndElement(); //Default

            //Van Route
            xmlWrt.WriteStartElement("", "Default", namespace);
            xmlWrt.WriteElementString("Label", namespace, "ROUTE");
            xmlWrt.WriteElementString("Value", namespace, "");  
            xmlWrt.WriteElementString("Iteration", namespace, "0");
            xmlWrt.WriteEndElement(); //Default

            //Get the orders from the man
            orderCount = 0;
            manOrder = new List<ManOrder>();

            //ERRORS NO WHERE CLAUSE in Error 1 'System.Collections.Generic.List does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Collections
            manOrder = manOrder.Where(w => w.conRef == vRef).ToList();

            foreach (ManOrder manLine in manOrder)
            {
                orderCount++;
                xmlWrt.WriteStartElement("", "Default", namespace);
                xmlWrt.WriteElementString("Label", namespace, "REF");
                xmlWrt.WriteElementString("Value", namespace, manLine.orderNumber);
                xmlWrt.WriteElementString("Iteration", namespace, orderCount.ToString());  
                xmlWrt.WriteEndElement(); //Default
            }

            for (int i = 0; i <= orderCount; i++)
            {
                xmlWrt.WriteStartElement("", "Default", namespace);
                xmlWrt.WriteElementString("Label", namespace, "VER");
                xmlWrt.WriteElementString("Value", namespace, "0"); 
                xmlWrt.WriteElementString("Iteration", namespace, i.ToString());  
                xmlWrt.WriteEndElement(); //Default
            }

            xmlWrt.WriteEndElement(); //Defaults                               

            //Panels
            xmlWrt.WriteStartElement("", "Panels", namespace);
            xmlWrt.WriteStartElement("", "Panel", namespace);
            xmlWrt.WriteElementString("Name", namespace, "Details");
            xmlWrt.WriteStartElement("", "Columns", namespace);

            //OrderReferences
            orderCount = 0;
            foreach (ManOrder manLine in manOrder)
            {
                orderCount++;
                xmlWrt.WriteStartElement("", "PanelColumn", namespace);
                xmlWrt.WriteElementString("Label", namespace, string.Format("Ref{0}", orderCount.ToString()));
                xmlWrt.WriteElementString("Value", namespace, manLine.orderNumber); 
                xmlWrt.WriteElementString("Iteration", namespace, "0");
                xmlWrt.WriteEndElement(); //PanelColumn
            }
        }

        string strDate = "";
        strDate = @"C:\Dump\" + "XML" + strDate + ".xml";
        xmlWrt.WriteEndElement();
        xmlWrt.WriteEndDocument();
        xmlWrt.Flush();
        memStream.Position = 0;
        using (FileStream file = new FileStream(strDate, FileMode.Create, System.IO.FileAccess.Write))
        {
            byte[] bytes = new byte[memStream.Length];
            memStream.Read(bytes, 0, (int)memStream.Length);
            file.Write(bytes, 0, bytes.Length);
            memStream.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我发现您可以使用FindAll方法并删除ToList。

直到SSIS中的.net 3.0才会使用Lambda表达式。

所以我的代码行是manOrder = manOrder.FindAll(w =&gt; w.conRef == vRef);

由于