我在C#中使用列表。我这样开课:
UserManager {
User User;
Other Variables;
}
User
类有一些变量,如:
User {
String Username, Password;
}
现在我要做的是,有一个UserManager
的列表并通过Username
找到它并且这样做了:
UserManager U = UM.Find(
item => item.Username == Username
);
但我想对Username
和Password
组合进行检查。我是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
);
BTW,Username
和Password
是String
来自用户Console.ReadLine()
。请帮我进一步......
答案 0 :(得分:2)
不要重复输入参数项
UserManager U = UM.Find(
item => item.Username == Username && item.Password == Password
);
答案 1 :(得分:0)
为什么不做呢
UserManager U = UM.Find(
item => item.Username == Username && item.Password == Password
);
你做错了
UserManager U = UM.Where(
item => item.Username == Username && item.Password == Password
);
答案 2 :(得分: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));
//....
}