Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async method that is returning bool

Tags:

c#

I have an Async method in C# that is calling SQL function which is returning true or false and method is returning bool variable that is true or false.

This is my method:

    public async Task<bool> Canlog(string userName, string pass
    {
        Context context = new Context();
        bool queryResult = false;

        using (context )
        {
            using (context .Database.Connection)
            {
                context .Database.Connection.Open();

                string sqlStatment = .....

                queryResult = authorizationContext.Database
                        .SqlQuery<bool>(sqlStatment)
                        .Single();
            }
        }

        return await queryResult;
    }

when I try this, I am getting error in this line return await queryResult that cannot await bool.

like image 429
Alma Avatar asked Feb 02 '26 16:02

Alma


1 Answers

Single() returns <T>, not Task<T>. What you want is SingleAsync():

    return await authorizationContext.Database
                                     .SqlQuery<bool>(sqlStatement)
                                     .SingleAsync();

Also, you can't have your await outside of your using statements because then your DB context will be disposed before the async operation is compelte.

like image 130
jhilden Avatar answered Feb 05 '26 04:02

jhilden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!