我在使用多个ifs时收到此错误“无效的表达式if”。我怎么能删除这个错误?

时间:2015-11-10 06:43:34

标签: c#

这是我必须编写的代码,但上面提到的错误是在第一个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);
                    }


                }

3 个答案:

答案 0 :(得分:1)

我宁愿消除所有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];

答案 1 :(得分:0)

您似乎忘了将值分配给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;
......

答案 2 :(得分:0)

您需要将值分配给变量。 请参考以下代码。 尝试使用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;
                }