Home > Enterprise >  Else statement never reach
Else statement never reach

Time:09-17

i have an if statements align with a query in Linq to create the login in my app, but, the code works bad, when the query don't select any record, the .Count() it's equal to zero and it have sense, but in the if, the code never reach the else part, look, this is my code:

var UserLogin = from m in _context.User where m.Username.Contains(this.Username) & m.Password.Contains(this.Password) select m.Username;
                this.Username = string.Empty;
                this.Password = string.Empty;
                if(UserLogin.Count() != 0)
                {
                    //HttpContext.Session.SetString("Username", UserLogin.First());
                    UsernameLogged = UserLogin.First();
                }
                else
                {
                    Console.WriteLine("Sisa: "   UsernameLogged);
                    this.Username = string.Empty;
                    this.Password = string.Empty;
                    UsernameLogged = string.Empty;
                    Console.WriteLine(UsernameLogged);
                }

CodePudding user response:

Note that the query is executed twice. The first time when you call UserLogin.Count(). The second time when you call UserLogin.First(). But then, you already have set Username and Password to string.Empty and all the user names and passwords do contain an empty string because String.Contains tests only whether a part of the string matches. So you are getting all the users in the result. Use == to test the whole string.

Since you want to get only one user, use FirstOrDefault which returns null when no user was found.

var userLogged = _context.User
    .FirstOrDefault(u => u.Username == Username && u.Password == Password);
if (userLogged is null) {
    Console.WriteLine("Sisa: "   UsernameLogged);
    Username = string.Empty;
    Password = string.Empty;
    UsernameLogged = string.Empty;
    Console.WriteLine(UsernameLogged);
} else {
    UsernameLogged = userLogged.Username;
}

You are setting Username and Password to string.Empty twice. Either clear them after the if-else if you always want to do so, or do it only when no user was found.

CodePudding user response:

This is because of you are using deferred execution of LINQ query. Your query is executed in if condition and there you will get count 1.

Use below code -

var UserLogin = from m in _context.User where m.Username.Contains(this.Username) & m.Password.Contains(this.Password) select m.Username;

var count = UserLogin.Count(); // Here your query will be execute

this.Username = string.Empty;
this.Password = string.Empty;

if(count != 0)
{
     //HttpContext.Session.SetString("Username", UserLogin.First());
     UsernameLogged = UserLogin.First();
}
else
{
     Console.WriteLine("Sisa: "   UsernameLogged);
     this.Username = string.Empty;
     this.Password = string.Empty;
     UsernameLogged = string.Empty;
     Console.WriteLine(UsernameLogged);
}

This will execute your else condition.

  • Related