搜索对象列表,然后检查对象特征的恶魔是否与c#匹配

时间:2016-03-09 19:06:11

标签: c# list object

我正在为uni写一个登录系统,我试图通过一个对象列表" allAdmin"

public class UserAdmin : User
{
    string mDept;
    string mJob;

    string mName;
    private string mPassword;
    string mEmail;

    public UserAdmin(string name, string pass, string dept, string job, string email)
        {
            mName = name;
            mPassword = pass;
            mDept = dept;
            mJob = job;
            mEmail = email;
        }

   public string GetDept()
    {
        return mDept;
    }

   public string GetName()
   {
       return mName;
   }

   public string GetPassword()
   {
       return mPassword;
   }

    public string GetJob()
    {
        return mJob;
    }
}

我正在使用以下代码

进行搜索
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;
    }

我将如何检查字段GobalVariables.allAdmin.username.GetPassword()是否匹配?

1 个答案:

答案 0 :(得分:0)

试试这个:

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;
}