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

时间:2017-07-09 20:19:38

标签: c# class foreach

我在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();基本上遍历所述XML文件并相应地添加值:

    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

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

将退货移出循环。

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;