0

我在 C# 中使用列表。我这样上课:

UserManager {
  User User;
  Other Variables;
}

该类User有一些变量,例如:

User {
  String Username, Password;
}

现在我想做的是,拥有一个UserManagers 列表并通过以下方式找到它Username

UserManager U = UM.Find(
  item => item.Username == Username
);

但我想同时检查两者UsernamePassword组合。我是 C# 新手,对如何实现感到困惑。试过这个但没有运气:

// Attempt #1
UserManager U = UM.Find(
  item => item.Username == Username && item => item.Password == Password
);

// Attempt #2
UserManager U = UM.Find(
  item => item.Username == Username, item => item.Password == Password
);

// Attempt #3
UserManager U = UM.Find(
  item => item.Username == Username, item.Password == Password
);

顺便说一句,Username并且PasswordString通过Console.ReadLine(). 请帮助我进一步进行...

4

3 回答 3

2

不要重复输入参数项

UserManager U = UM.Find(
  item => item.Username == Username && item.Password == Password
);
于 2013-10-27T18:25:40.097 回答
0

为什么不只是做

UserManager U = UM.Find(
item => item.Username == Username && item.Password == Password
);

做不到你能做到

UserManager U = UM.Where(
item => item.Username == Username && item.Password == Password
);
于 2013-10-27T18:25:29.997 回答
0

您可以尝试以下方法:

if(Users.Any(user => user.UserName.Equals(UserName) &&
                                 user.Password.Equals(Password)) {

     var matchedUser = Users.Where(user => user.UserName.Equals(UserName) &&
                                 user.Password.Equals(Password));

     //....
    }
于 2013-10-27T18:29:41.670 回答