简体   繁体   中英

LINQ query Using Entity Framework in C# to get Single Column & Single Row Value Without using Where

LINQ query to get Single Column & Single Row value.

Creating a login module using SHA512 in MVC4 . I have two tables as UserMaster & tblSalt . From UserMaster I have retrieved all the fields but in tblSalt table there is only one column with one row, therefore can't use Where condition. How to access that column?

//Retrive Stored HASH Value From Database According To Username (one unique field)  
var userInfo = db.UserMasters.Where(s => s.Username == entity.Username.Trim()).FirstOrDefault();  
var saltVAL = db.tblsalts.Max(s => s.saltvalue.ToString().Trim()).FirstOrDefault();
//Assign HASH Value  
if (userInfo != null)  
{  
    OLDHASHValue = userInfo.Hashpwd;  
    SALT = saltVAL.  ;  
}

Expected to access saltvalue column from saltVAL variable & get initialized to SALT variable.

Use FirstOrDefault() to grab the first row from your table (or null if the table is empty)

var saltVAL = db.tblsalts.FirstOrDefault()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM