满足列表中的条件后进行Foreach

时间:2018-09-12 09:01:28

标签: c#

我正在使用列表创建静态登录页面以保存数据。我正在使用ForEach遍历列表,但是我面临的问题是我希望我的for循环在条件为true时立即停止。

注意:我已经尝试过使用休息和退货,但是它们没有按预期工作。

代码在这里:

List<User> users = new List<User>(3);
    public MainWindow()
    {
        InitializeComponent();

        User superAdmin = new User()
        {
            userType = "Super Admin",
            uniqueCode = "123456",
            password = "password1"
        };
        User admin = new User()
        {
            userType = "Admin",
            uniqueCode = "654321",
            password = "password16"
        };
        User userOperator = new User()
        {
            userType = "Operator",
            uniqueCode = "109105",
            password = "specialpassword"
        };

        users.Add(superAdmin);  
        users.Add(admin);
        users.Add(userOperator);
    }



    private void login_OnClick(object sender, RoutedEventArgs e)
    {
        string userType = cmbAdminType.Text;
        string uniqueCode = txtUniqueCode.Text;
        string password = txtPassword.Text;

        foreach (User userPick in users)
        {        
            if (userPick.userType == userType && userPick.uniqueCode == uniqueCode)
            {
                MessageBox.Show("Cool you are in!");
                break;
            }
            else
            {
                MessageBox.Show("Err, not found!");
                break;
            }

        }

    }
}
    public class User
    {
        public string userType { get; set; }
        public string uniqueCode { get; set; }
        public string password { get; set; }
    }

请,我还能做什么?

3 个答案:

答案 0 :(得分:4)

其他人指出了您当前代码中的缺陷,但是我建议在这里使用LINQ方法。它更短,更容易阅读-至少在您熟悉LINQ时:

bool validUser = users.Any(user => user.userType == userType && user.uniqueCode == uniqueCode);
MessageBox.Show(validUser ? "Cool you are in!" : "Err, not found!");

Any处于短路状态:一旦找到匹配项,它就会停止。

作为一个旁注,我强烈建议您开始为属性遵循.NET命名约定。

答案 1 :(得分:2)

我相信这可能是因为您在代码的工作方式上存在逻辑错误。

当前,您在第一个成功比赛或第一个未成功比赛中退出了foreach循环。因此,基本上,无论成功与否,枚举都会在1次迭代后中断。

您可以引入一个用于记录是否记录成功的标志,然后在枚举之后对其进行测试,如下所示:

private void login_OnClick(object sender, RoutedEventArgs e)
{
    string userType = cmbAdminType.Text;
    string uniqueCode = txtUniqueCode.Text;
    string password = txtPassword.Text;
    bool isMatched = false;

    foreach (User userPick in users)
    {        
        if (userPick.userType == userType && userPick.uniqueCode == uniqueCode)
        {
            isMatched = true;
            break;
        }
    }

    if (isMatched)
    {
        MessageBox.Show("Cool you are in!");
    }
    else
    {
        MessageBox.Show("Err, not found!");
    }
}

答案 2 :(得分:0)

您的循环在列表中找到第一个对象之后停止。我认为您想要完成的是,如果未找到要查找的对象,则在循环结束时找到要查找的对象时,它就会中断。代码:

bool userInside = false;
foreach (User userPick in users)
    {        
        if (userPick.userType == userType && userPick.uniqueCode == uniqueCode)
        {
            MessageBox.Show("Cool you are in!");
            userInside=true;
            break;
        }                
    }
if (userInside==false)
    {
        MessageBox.Show("Err, not found!");
    }

我希望这会有所帮助。 ^^