Milan90 Milan90 - 1 month ago 7
ASP.NET (C#) Question

Getting entity element by Name

I'm trying to create a login form with ASP.NET and entity framework.
And I have something like this:

Account account2 = de.Accounts.Find(accountViewModel.Account.Username);
if(account2 != null)
{
if(PasswordHelper.VerifyHash(accountViewModel.Account.Password, "SHA512", account2.Password))
{
Session["username"] = account2.Username;
return View("intro");
}


I know that the find method is to search items by Primary Key. My Primary key is "AccountID", but how can i find element by "Account" ? PLS HELP :((

Answer

You mean find account by account number? Whatever account property is used to identify the account, the following linq may be useful:

using System.Linq;

....
Account account2 = de.Accounts.FirstOrDefault(a => a.Prop == value);

for example:

Account account2 = de.Accounts.FirstOrDefault(a => a.UserName == accountViewModel.Account.Username);