C# 如何使用字符串参数而不是find(主键)函数从dbcontext中查找对象
是的,context.Accounts.Find(id)其中id是主键,它确实为我带来了正确的帐户。 但我想从context.Accounts中搜索用户名字符串,而不是主键 更准确地说,方法:C# 如何使用字符串参数而不是find(主键)函数从dbcontext中查找对象,c#,asp.net-mvc,entity-framework,dbcontext,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;
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;
}
所以用户名不再是主键了,没有find()函数我如何才能找到正确的用户名?替换
Accounts test = new Accounts();
test.User = username;
Accounts dbEntry = new Accounts();
dbEntry = context.Accounts_.Find(test.User);
与
var dbEntry = context.Accounts_.FirstOrDefault(acc => acc.username == username);
或者,无论在您的帐户中调用什么“用户名”\u您知道如何获取具有相同值的对象列表吗?@FaizEming使用
Where
子句<代码>上下文.Accounts.Where(x=>x.Blah==“Blah”).ToList()