繁体   English   中英

循环遍历一个类并将每一行添加到DataTable中只会添加一行

[英]Looping through a class and adding each row to a DataTable only adds one row

我在C#中有以下循环:

        PayrollItem[] it = GetPayrollItems(xmlResult);

        foreach (var inv in it)
        {
            DataRow row = ResultsTable.NewRow();
            row["EmployeeKey"] = inv.BadgeNo;
            row["StoreKey"] = inv.CostCentre;
            row["EmployeeName"] = inv.EmpName;
            row["DateKey"] = inv.Date;
            row["Incheck"] = inv.In;
            row["Outcheck"] = inv.Out;
            System.Diagnostics.Debug.WriteLine("inv.BadgeNo {0} i StoreKey {1}", inv.EmpName, inv.CostCentre);
            ResultsTable.Rows.Add(row);
            return ResultsTable;
        }

其中xmlResult是一个XML文件,其中填充了使用SOAP API调用获得的数据,以及GetPayrollItems(); 基本上遍历所述XMLfile并相应地添加值:

    public static PayrollItem[] GetPayrollItems(XmlDocument xmlDoc)
    {
        var items = new List<PayrollItem>();

        var node = xmlDoc.LastChild; //SOAP-ENV:Envelope
        if (node != null)
        {
            node = node.FirstChild; //SOAP-ENV:Body
            if (node != null)
            {
                node = node.FirstChild; //ns1:wsdlGetValuesResponse
                if (node != null)
                {
                    node = node.FirstChild;  //return

                    if (node != null)
                    {
                        int count = node.ChildNodes.Count;

                        foreach (XmlNode nodeItem in node.ChildNodes)
                        {
                            PayrollItem item = new PayrollItem();

                            item.EmpName = nodeItem["empName"].InnerText;
                            item.BadgeNo = nodeItem["badgeNo"].InnerText;
                            item.CostCentre = nodeItem["costCentre"].InnerText;
                            item.Date = nodeItem["date"].InnerText;
                            item.In = nodeItem["in"].InnerText;
                            item.Out = nodeItem["out"].InnerText;
                            items.Add(item);
                        }
                    }
                }
            }
        }

        return items.ToArray();
    }

我将PayrollItems定义为:

   public class PayrollItem 
    {
        public string EmpName { get; set; }
        public string BadgeNo { get; set; }
        public string Date { get; set; }
        public string In { get; set; }
        public string Out { get; set; }
        public string CostCentre { get; set; }
    }

    public class PayrollList : List<PayrollItem>
    {
        public void Add(string empName, string badgeNo, string date, string @in, string @out, string costCentre)
        {
            var data = new PayrollItem
                {
                    EmpName = empName,
                    BadgeNo = badgeNo,
                    Date = date,
                    In = @in,
                    Out = @out,
                    CostCentre = costCentre
                };
            this.Add(data);
        }
    }

我调用该函数GetPayrollItems从功能GetPayroll 此函数带有一些参数,其中的一个是特定商店的API密钥。

我注意到, GetPayrollItems()函数有时每次迭代都会返回20行,即对于一个商店。 然而,当我检查表ResultsTable的程序执行时,我注意到,只有一行将被加到每个商店。

当我测试将行添加到ResultsTable的循环时,我注意到它有时在一次迭代中最多包含20行,但是仍然仅将一行添加到ResultsTable

有谁知道为什么会这样吗?

将返回移出循环。

PayrollItem[] it = GetPayrollItems(xmlResult);

    foreach (var inv in it)
    {
        DataRow row = ResultsTable.NewRow();
        row["EmployeeKey"] = inv.BadgeNo;
        row["StoreKey"] = inv.CostCentre;
        row["EmployeeName"] = inv.EmpName;
        row["DateKey"] = inv.Date;
        row["Incheck"] = inv.In;
        row["Outcheck"] = inv.Out;
        System.Diagnostics.Debug.WriteLine("inv.BadgeNo {0} i StoreKey {1}", inv.EmpName, inv.CostCentre);
        ResultsTable.Rows.Add(row);

    }

return ResultsTable;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM