How can I find a object from dbcontext with string parameter, not with Find(primary key) function

Jon*_*ula 2 c# asp.net-mvc entity-framework dbcontext

是的, context.Accounts.Find(id) ,其中 id 是主键,它确实为我带来了正确的帐户。但我想从 context.Accounts 中使用字符串用户名进行搜索,而不是按主键。

更准确地说,方法:

public void Authenticate(string username, password)
{       result = false;
        Accounts test = new Accounts();
        test.User = username;
        Accounts dbEntry = new Accounts();
        dbEntry = context.Accounts_.Find(test.User);

        if (dbEntry.Password == password)
            if(dbEntry.Admin == 1)
              result = true;
        return result;
    }
Run Code Online (Sandbox Code Playgroud)

那么用户名不再是主键,我如何在没有 Find() 函数的情况下找到正确的用户名?

Jon*_*lis 5

代替

    Accounts test = new Accounts();
    test.User = username;
    Accounts dbEntry = new Accounts();
    dbEntry = context.Accounts_.Find(test.User); 
Run Code Online (Sandbox Code Playgroud)

var dbEntry = context.Accounts_.FirstOrDefault(acc => acc.username == username);
Run Code Online (Sandbox Code Playgroud)

或您帐户中的任何“用户名”_