C# 搜索对象列表,然后检查对象特征的恶魔是否与c匹配#
我正在为uni编写一个登录系统,我正在尝试搜索对象“allAdmin”的列表 我正在使用以下代码进行搜索C# 搜索对象列表,然后检查对象特征的恶魔是否与c匹配#,c#,list,object,C#,List,Object,我正在为uni编写一个登录系统,我正在尝试搜索对象“allAdmin”的列表 我正在使用以下代码进行搜索 public string LoginAdmin(string username, string password) { //Lookup users in Admin Group int result = -1; int numUsers = GlobalVariables.allAdmin.Count(); fo
public string LoginAdmin(string username, string password)
{
//Lookup users in Admin Group
int result = -1;
int numUsers = GlobalVariables.allAdmin.Count();
for (int loopCount = 0; loopCount < numUsers ; loopCount++)
{
bool correctuser = GlobalVariables.allAdmin.Any(x => x.getName() == username);
if (correctuser == true)
{
result = loopCount;
//check if GlobalVariables.allAdmin.username.GetPassword() == password;
break;
}
}
return result;
}
publicstringloginadmin(字符串用户名、字符串密码)
{
//查找管理员组中的用户
int结果=-1;
int numUsers=GlobalVariables.allAdmin.Count();
对于(int-loopCount=0;loopCountx.getName()==username);
if(correctuser==true)
{
结果=循环计数;
//检查GlobalVariables.allAdmin.username.GetPassword()==密码;
打破
}
}
返回结果;
}
如何检查字段GobalVariables.allAdmin.username.GetPassword()是否匹配?尝试以下操作:
public string LoginAdmin(string username, string password)
{
var user = GlobalVariables.allAdmin
.FirstOrDefault(x => x.getName() == username && x.GetPassword() == password);
if(user==null) throw new AuthorizationException("...")
var result = GlobalVariables.allAdmin.IndexOf(user);
return result;
}
public string LoginAdmin(string username, string password)
{
var user = GlobalVariables.allAdmin
.FirstOrDefault(x => x.getName() == username && x.GetPassword() == password);
if(user==null) throw new AuthorizationException("...")
var result = GlobalVariables.allAdmin.IndexOf(user);
return result;
}