字符串中的 C# 多个 switch case 组合

我最近在将经典 ASP 应用程序转换为 C# .NET 时遇到了一个相当有趣的问题。在这个 MVC 应用程序中,前端复选框(如下)作为字符串读入,可以有多种组合。AC# case 语句确定正确的组合,并将 case 语句中的 SQL 语句连接到数据层文件中的 SELECT。


<input value="A" type="checkbox" class="material-checkbox" 

id="" name="checkbox" checked />

<input value="B" type="checkbox" class="material-checkbox" 

id="" name="checkbox" />

<input value="C" type="checkbox" class="material-checkbox" 

id="" name="checkbox" />

<input value="D" type="checkbox" class="material-checkbox" 

id="" name="checkbox" />


倚天杖
浏览 129回答 4
4回答

猛跑小猪

尝试这个:有多种方法可以做到这一点:ABCD 的组合可以创建总共 15 种组合。我在开关盒中创建了所有 15 种组合:string checkboxvalue = "";if(chkA.Checked()) checkboxvalue +="A";if(chkB.Checked()) checkboxvalue +="B";if(chkC.Checked()) checkboxvalue +="C";if(chkD.Checked()) checkboxvalue +="D";switch(checkboxvalue)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case "ABCD":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "ABC":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "ABD":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "ACD":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "BCD":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "AB":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "AC":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "AD":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "BC":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "BD":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "CD":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "A":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "B":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "C":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "D":&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }

慕侠2389804

假设用户选择框 A、C、D。strSql += "SELECT column1, column2, ...&nbsp; &nbsp; &nbsp; FROM table_name;"&nbsp; &nbsp; &nbsp;switch(checkboxvalue)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case (("ABCD")): //<---Contains "ACD" but not "B" NO MATCH&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND .....;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case (("ABC")): //<---- Contains "A" but not "BC" NO MATCH&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND.......;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case (("AB")): //<--Contains "A" but no "B" NO MATCH&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND......;"&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; case (("AC")): //<--Contains "AC" but no "D" NO MATCH&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND.....";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case (("ACD")): //<--MATCH!&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; strSql += "AND......"; //<--add to select to pull correct data&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }

largeQ

由于您基本上是在比较布尔值,因此我建议您制作一个表格来映射您拥有的不同复选框组合,以验证您是否会更好地使用 if-else 或使用少数嵌套 if 而不是开关。尝试重新考虑您将拥有多少输出以及在这种情况下需要执行哪些操作。static void executeSQL(string checkboxes){&nbsp; &nbsp; switches checkbox_switches = new switches(checkboxes.Contains('A'), checkboxes.Contains('B'), checkboxes.Contains('C'), checkboxes.Contains('D'));&nbsp; &nbsp; if (checkbox_switches.action_A)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Execute SQL&nbsp; &nbsp; }&nbsp; &nbsp; else if (checkbox_switches.action_B)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Execute SQL&nbsp; &nbsp; }&nbsp; &nbsp; else if (checkbox_switches.action_C)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Execute SQL&nbsp; &nbsp; }&nbsp; &nbsp; else if (checkbox_switches.action_D)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Execute SQL&nbsp; &nbsp; }&nbsp; &nbsp; else if (checkbox_switches.action_E)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Execute SQL&nbsp; &nbsp; }}struct switches{&nbsp; &nbsp; public bool action_A, action_B, action_C, action_D, action_E;&nbsp; &nbsp; public switches(bool A, bool B, bool C, bool D)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; action_A = A && B;&nbsp; &nbsp; &nbsp; &nbsp; action_B = B || D;&nbsp; &nbsp; &nbsp; &nbsp; action_C = C && D;&nbsp; &nbsp; &nbsp; &nbsp; action_D = action_A && C;&nbsp; &nbsp; &nbsp; &nbsp; action_E = !A && !B && !C && D;&nbsp; &nbsp; }}

Helenr

我假设您可能正在尝试根据选中的复选框选项构建 where 子句条件。在这里,如果我们尝试使用 switch 语句,我们可能最终会为每个组合编写很多案例,这不会是有效的(比如 - a, ab, abc, abcd, ac, acd, ad, b , bc, bcd, bd, c, cd, d)。如果这些值不是按顺序排列的,那么该组合将成倍增加。更糟。因此,使用带有字符串构建器的简单条件语句来保存最终字符串将是有益的,如下所示 -StringBuilder sb = new StringBuilder();if (checkbox1.checked){&nbsp; &nbsp; sb.Apppend("Condition1");}if (checkbox2.checked){&nbsp; &nbsp; sb.Apppend("Condition2");}if (checkbox3.checked){&nbsp; &nbsp; sb.Apppend("Condition3");}if (checkbox4.checked){&nbsp; &nbsp; sb.Apppend("Condition4");}
打开App,查看更多内容
随时随地看视频慕课网APP