1

I'm trying to make login query in sqlite but can't convert AsyncTableQuery in boolean

public bool queryLogIn(string userNameLogIn,string passwordLogIn)
{
    var query=database.Table<Users>().Where(i => i.UserName == userNameLogIn && i.Password == passwordLogIn);

    if (query == true)//There is error
    {
        return true;
    }
    else
    {
        return false;
    }                
}
1
  • Hi. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the gray check mark beside the answer. Check this link to know How does accepting an answer work: meta.stackexchange.com/questions/5234/… Commented Dec 16, 2017 at 18:55

2 Answers 2

1

You can use FirstOrDefault function for login is success.

public bool queryLogIn(string userNameLogIn,string passwordLogIn)
{
var query=database.Table<Users>().FirstOrDefault(i => i.UserName == userNameLogIn && i.Password == passwordLogIn);

if (query == null)//There is error
{
    return false;
}
else
{
    return true;
}                
}
Sign up to request clarification or add additional context in comments.

1 Comment

FirstOrDefault has no argument
1

Where returns an iqueryable of Users. You need Any instead which Determines whether any element of a sequence satisfies a condition and since it's return type is bool you can use it in your if statement:

var query=database.Table<Users>().Any(i => i.UserName == userNameLogIn
                                           && i.Password == passwordLogIn);

EDIT: Or use Any in your if:

var query=database.Table<Users>().Where(i => i.UserName == userNameLogIn 
                                             && i.Password == passwordLogIn);
if (query.Any())
{
    return true;
}

5 Comments

how to that query ?
I'm sorry there is no .Any(). I can do query.CountAsyn() , .toListAsync() , .take() , .skip() ,.equals()... but no .Any(). However thank you very much
@KosovoDev What about .toListAsync().Any() ?
@KosovoDev are you sure that you've added using System.Linq; to your using directives?
Yes but no Any() method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.