I'm still learning LINQ. I can do some simple queries, but this one is a little different. I have the following query that authenticates a user.
User user = (from u in db.Users
where u.Username.Equals(username) &&
u.Password.Equals(UserSecurity.GetPasswordHash(username, password)) &&
u.Status == true
select u).FirstOrDefault();
This works but now I need to log why the login failed. If there was an invalid username, I'll log one thing. If the password was wrong, I'll log another. Or, if the user is inactive (status = false), then I'll log another. I realize I can separate this out into 3 separate queries to figure out what failed, but I was wondering if it would be more efficient to do it in one. If so, how? This is my (untested) 3 query approach.
User user2 = null;
var users1 = db.Users.Where(i => i.Username.Equals(username));
if (users1 != null)
{
var users2 = users1.Where(i => i.Password.Equals(UserSecurity.GetPasswordHash(username, password)));
if (users2 != null)
{
var users3 = users2.Where(i => i.Status);
if (users3 != null)
{
user2 = users3.FirstOrDefault();
}
else
{
// User inactive
}
}
else
{
// Password invalid.
}
}
else
{
// Username invalid
}