如何在 C# 中使用多个 IF 条件

我想在不同的 SESSIONS 中存储多个值,因为我在 VB 代码中有多个可以正常工作的 If 条件


If roleRow.Item("Code").ToString() = "101" Then 'Admin Settings

    If roleRow.Item("Active") = True Then Session("101") = True Else Session("101") = False

End If

If roleRow.Item("Code").ToString() = "102" Then 'Summaries / Reports

    If roleRow.Item("Active") = True Then Session("102") = True Else Session("102") = False

End If

If roleRow.Item("Code").ToString() = "103" Then 'Invoices List

    If roleRow.Item("Active") = True Then Session("103") = True Else Session("103") = False

End If

但相同的代码结构在 C# 中不起作用。


它仅评估第一个 IF 条件和其余条件,它显示与第一个 IF 条件相同的值。任何建议如何解决这个问题?


private void UserPermissions_Read()

{             

    BOL.Master.UsersRoles aDA = new BOL.Master.UsersRoles();

    DAL.Master.UsersRoles.UsersRolesDataTable aDT = new 

    DAL.Master.UsersRoles.UsersRolesDataTable();

    aDT = aDA.Read("1", 1, "", 1, "", "", "", "");

    DataRow dRow;

    if (aDT.Rows.Count > 0)

    {

        dRow = aDT.Rows[0];              

        if (dRow["Code"].ToString() == "101" && (Boolean)dRow["Active"] == true)

        {

            Session["101"] = true;

        }

        else

        {

            Session["101"] = false;

        }               

        if (dRow["Code"].ToString() == "102" && (Boolean)dRow["Active"] == true)

        {

            Session["102"] = true;

        }

        else

        {

            Session["102"] = false;

        }                

    } 

    else 

    {

        Session["101"] = false;

        Session["102"] = false;

        Session["103"] = false;

    }

}


尚方宝剑之说
浏览 331回答 2
2回答

婷婷同学_

目前还不清楚你想要完成什么,但我建议你多考虑这个问题,然后再考虑“翻译”。这是否更接近您真正想要实现的目标?//First I'll create an array of every value to be inspected and iterate through it....foreach (var c in new[] {"104", "105", "106"}){    //Then I'll check the current value (considering the value of dRow["Active"]...    Session[c] = ( ((bool)dRow["Active"]) && (dRow["Code"].ToString() == c) )  //Thanks to @jjwillmc observation}如果您更喜欢仅使用 IF,则 VB 代码转换如下:If roleRow.Item("Code").ToString() = "101" Then 'Admin Settings    If roleRow.Item("Active") = True Then Session("101") = True Else Session("101") = FalseEnd IfIf roleRow.Item("Code").ToString() = "102" Then 'Summaries / Reports    If roleRow.Item("Active") = True Then Session("102") = True Else Session("102") = FalseEnd IfIf roleRow.Item("Code").ToString() = "103" Then 'Invoices List    If roleRow.Item("Active") = True Then Session("103") = True Else Session("103") = FalseEnd If变成:if (roleRow.Item("Code").ToString() == "101") //Admin Settings{    if (roleRow.Item("Active"))       Session["101"] = true;     else       Session["101"] = False;    //Or simply: Session["101"] = (roleRow.Item("Active"));}if (roleRow.Item("Code").ToString() == "102") //Summaries / Reports{    if (roleRow.Item("Active"))       Session["102"] = true;     else       Session["102"] = false;    //Or simply: Session["102"] = (roleRow.Item("Active"));}if (roleRow.Item("Code").ToString() == "103") //Invoices List{    if (roleRow.Item("Active"))       Session["103"] = true;    else       Session["103"] = false;    //Or simply: Session["103"] = (roleRow.Item("Active"));}希望能帮助到你。

呼如林

完成......在代码中缺少For循环......这样它就会读取每一行并将每一行值放入不同的SESSION......也在此行之前 DataRow dRow; 您必须将每个 SESSION = False 声明为默认值....DataRow dRow;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (aDT.Rows.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i <= aDT.Rows.Count -1; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dRow = aDT.Rows[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dRow["Code"].ToString() == "101" && (Boolean)dRow["Active"] == true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session["101"] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (dRow["Code"].ToString() == "102" && (Boolean)dRow["Active"] == true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session["102"] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (dRow["Code"].ToString() == "103" && (Boolean)dRow["Active"] == true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session["103"] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP