c#do if语句中断循环

时间:2014-10-06 20:52:06

标签: c# if-statement foreach nested break

数据源:具有员工信息的数据表

我尝试按员工姓名对数据进行分组,然后输出员工姓名,并输出与该员工相关的记录,并仅输出与该员工相关的记录。

我在每个嵌套中都有一个if语句,每个都在另一个嵌套中(我也尝试过常规for循环)。 if语句根据它所迭代的当前名称检查员工姓名,如果是真,则输出记录的信息。这适用于每位员工的第一条记录。在每个员工的第一次迭代之后它被打破并且消除过程已经向我展示了它是因为if语句。就像使用if语句打破循环一样。


public String GroupAndSortTableData(DataTable thisDataTable)
    {
        //detailActivitiesDataTable datatable column key
        //0 = currentID, 1 = formerID, 2 = Description, 3 = MonthID, 4 = Year, 5 = CreatedBy, 6 = CreatedOn, 7 = ModifiedBy, 
        //8 = ModifiedOn, 9 = UserID, 10 = Signed, 11 = Type, 12 = Software, 13 = Activity, 14 = DeleteFlag, 15 = DeletedBy, 16 = DeletedOn
        //17 = UserName, 18 = SortName, 19 = FullName
        try
        {
            System.Text.StringBuilder strReport = new System.Text.StringBuilder();
            //get unique values in the SortName column of detailActivitiesDataTable
                         group row by row.Field<string>("SortName") into grp
                         select new
                         {
                             SortName = grp.Key,
                             //MemberCount = grp.Count()
                         };
            strReport.AppendLine("<table width='800px' class='light' cellspacing='0' cellpadding='0' border='0'><tr><th align='left' colspan='4'> A C T I V I T I E S</th></tr></table>");
            strReport.AppendLine("<table width='800px' class='light' cellspacing='0' cellpadding='0' border='0'><tr><th align='left' colspan='4'><hr></th></tr></table>");
            //display a subheader bar for each name in the data
            foreach (var uniqueName in result)
            {
                //testing messagebow works in localhost only    
                strReport.AppendLine("<table class='dark' width='800px'><tr><th colspan='4' align='left'>" + uniqueName.SortName + "</th></tr>");
                strStaffActivities.AppendLine("<tr><td colspan='4' align='left'><hr width='800px'></td></tr>");
                strReport.AppendLine("<tr>"
                                                + "<td class='lightHeader2' width='150'>Type</td>"
                                                + "<td class='lightHeader2' width='150'>Activity</td>"
                                                + "<td class='lightHeader2' width='150'>Software</td>"
                                                + "<td class='lightHeader2' width='350'>Description</td>"
                                                + "</tr></table>");
                strReport.AppendLine("<table class='light' width='800px'>");
                //loop through the rows of the datatable 
                foreach(DataRow dtRow in thisDataTable.Rows)
                {
                    //if it matches the username display the content
                    if (dtRow[18] == uniqueName.SortName)
                    {
                        strReport.AppendLine("<tr>");
                        strReport.AppendLine("<td class='light' width='150'>" + dtRow[11] + "</td>");
                        strReport.AppendLine("<td class='light' width='150'>" + dtRow[13] + "</td>");
                        strReport.AppendLine("<td class='light' width='150'>" + dtRow[12] + "</td>");
                        strReport.AppendLine("<td class='light' width='350'>" + dtRow[2] + "</td>");
                        strReport.AppendLine("<td class='light' width='350'>" + dtRow[18] + "</td>");
                        strReport.AppendLine("<td class='light' width='350'>" + uniqueName.SortName + "</td>");
                        strReport.AppendLine("</tr>");
                    }
                    else
                        continue;
                }

            }
            strReport.AppendLine("</table>");
            return strReport.ToString();
        }
        catch (Exception eGroupActivitiesDetail)
        {
            var resultException = MessageBox.Show("Exception occurred while grouping and sorting data." + eGroupActivitiesDetail);
            return "failure";
        }
    }

2 个答案:

答案 0 :(得分:2)

if语句可以&#34;打破循环&#34;如果其条件包含触发异常的指令。

你的if (dtRow[18] == uniqueName.SortName)是一个很好的嫌疑人,可以产生一个超出界限的指数&#34; dtRow在索引18处没有元素的情况下的异常。在引用它之前,应该添加一个检查以查看索引是否有效,以避免抛出异常。

答案 1 :(得分:0)

找到答案,数据表中有错误的数据。