我得到了这个错误“;如果“;同时使用多个if.如何删除此错误

本文关键字:错误 何删除 if 删除 如果 | 更新日期: 2023-09-27 18:21:28

这是我必须编写的代码,但上面提到的错误是在第一个if中抛出的。我如何删除这个错误?

for (int i = 0; i < dt.Rows.Count; i++)
                {
                    try
                    {
                        comment = dt.Rows[i]["Comment"].ToString();
                        dt.Rows[i]["Comment"] = comment.Replace(System.Environment.NewLine, " ");
                        data = "2" + "|" +
                            dt.Rows[i]["XXXXXXXXXX"].ToString() + "|" +
                            dt.Rows[i]["XXXXXXXXXX"].ToString() + "|" +
                            dt.Rows[i]["XXXXXXXXXX"].ToString() + "|" +
                            dt.Rows[i]["XXXXXXXXXX"].ToString() + "|" +
                            dt.Rows[i]["XXXXXXXXXX"].ToString() + "|" +
                        if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "16")
                            {
                                "26" + "|||";
                            }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "1")
                                {
                                    "DA" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "2")
                                {
                                    "PC" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "3")
                                {
                                    "CC" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "22")
                                {
                                    "PI" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "26")
                                {
                                    "ED" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "27")
                                {
                                    "20" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "28")
                                {
                                    "ED" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "30")
                                {
                                    "MI" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "31")
                                {
                                    "12" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "32")
                                {
                                    "21" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "33")
                                {
                                    "21" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "34")
                                {
                                    "21" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "35")
                                {
                                    "MB" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "49")
                                {
                                    "NP" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "51")
                                {
                                    "NP" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "53")
                                {
                                    "NP" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "100")
                                {
                                    "CP" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "140")
                                {
                                    "12" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "141")
                                {
                                    "ED" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "149")
                                {
                                    "MB" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "177")
                                {
                                    "MI" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "178")
                                {
                                    "MI" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "179")
                                {
                                    "MI" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "180")
                                {
                                    "MI" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "200")
                                {
                                    "20" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "202")
                                {
                                    "NP" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "204")
                                {
                                    "15" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "224")
                                {
                                    "12" + "|||";
                                    }
                            else if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "227")
                                {
                                    "MI" + "|||";
                                    }

                        using (StreamWriter sw = File.AppendText(StmtqltyFile))
                        {
                            sw.WriteLine(data);
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteLog("Error: " + ex.Message);
                    }

                }

我得到了这个错误“;如果“;同时使用多个if.如何删除此错误

我宁愿消除所有的if,并将相应的值放入字典

private static Dictionary<String, String> s_RowToValue = new Dictionary<String, String>() {
  {"16", "26|||"},
  {"2", "PC|||"}, 
  ... 
  {"227", "MI|||"},
};

在这种情况下,所有的if都将变成

String key = dt.Rows[i]["XXXXXXXXXX"].ToString();
if (s_RowToValue.ContainsKey(key))
  data += s_RowToValue[key];

看起来您忘记将值分配给if 中的某个变量

if (dt.Rows[i]["XXXXXXXXXX"].ToString() == "16")
{
     somevar = "26" + "|||";
}

我建议你使用switch case,而不是像这样使用多个if else

switch (dt.Rows[i]["XXXXXXXXXX"].ToString())
case "16" : somevar = "26" + "|||";
            break;
case "1"  : somevar = "DA" + "|||";
            break;
case "2"  : somevar = "PC" + "|||";
            break;
case "3"  : somevar = "CC" + "|||";
            break;
......

您需要将值分配给一个变量。请参考以下代码。尝试使用switch case而不是if else

                              var data = "2" + "|" +
                               dt.Rows[i]["XXXXXXXXXX"] + "|" +
                               dt.Rows[i]["XXXXXXXXXX"] + "|" +
                               dt.Rows[i]["XXXXXXXXXX"] + "|" +
                               dt.Rows[i]["XXXXXXXXXX"] + "|" +
                               dt.Rows[i]["XXXXXXXXXX"] + "|";
                switch (dt.Rows[i]["XXXXXXXXXX"].ToString())
                {
                    case "16":
                        data += "26" + "|||";
                        break;
                    case "1":
                        data += "DA" + "|||";
                        break;
                    case "2":
                        data += "PC" + "|||";
                        break;
                    case "3":
                        data += "CC" + "|||";
                        break;
                    case "22":
                        data += "PI" + "|||";
                        break;
                    case "26":
                        data += "ED" + "|||";
                        break;
                    case "27":
                        data += "20" + "|||";
                        break;
                    case "28":
                        data += "ED" + "|||";
                        break;
                    case "30":
                        data += "MI" + "|||";
                        break;
                    case "31":
                        data += "12" + "|||";
                        break;
                    case "32":
                        data += "21" + "|||";
                        break;
                    case "33":
                        data += "21" + "|||";
                        break;
                    case "34":
                        data += "21" + "|||";
                        break;
                    case "35":
                        data += "MB" + "|||";
                        break;
                    case "49":
                        data += "NP" + "|||";
                        break;
                    case "51":
                        data += "NP" + "|||";
                        break;
                    case "53":
                        data += "NP" + "|||";
                        break;
                    case "100":
                        data += "CP" + "|||";
                        break;
                    case "140":
                        data += "12" + "|||";
                        break;
                    case "141":
                        data += "ED" + "|||";
                        break;
                    case "149":
                        data += "MB" + "|||";
                        break;
                    case "177":
                        data += "MI" + "|||";
                        break;
                    case "178":
                        data += "MI" + "|||";
                        break;
                    case "179":
                        data += "MI" + "|||";
                        break;
                    case "180":
                        data += "MI" + "|||";
                        break;
                    case "200":
                        data += "20" + "|||";
                        break;
                    case "202":
                        data += "NP" + "|||";
                        break;
                    case "204":
                        data += "15" + "|||";
                        break;
                    case "224":
                        data += "12" + "|||";
                        break;
                    case "227":
                        data += "MI" + "|||";
                        break;
                }