Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bool type return rule

I use dapper ORM.So i use two rules Query<T> & QuerySingle<T>. Query return the list & QuerySingle return the single object.

So,I want to get a bool type. (Actually I wanted to get a bool is true or false record).

My Query<T>:

public IEnumerable<T> Query<T>(string SqlString) where T : class
{
    return this.conn.Query<T>(SqlString);
}

So how can I write bool type return?

like image 905
TechGuy Avatar asked Jul 07 '15 18:07

TechGuy


People also ask

How to return a boolean value from a Boolean expression?

However, it is more common to return boolean values from boolean expressions, for conditional testing (see below). A Boolean expression is a Java expression that returns a Boolean value: true or false. You can use a comparison operator, such as the greater than ( >) operator to find out if an expression (or a variable) is true:

What is a bool variable in C++?

bool (C++) This keyword is a built-in type. A variable of this type can have values true and false. Conditional expressions have the type bool and so have values of type bool. For example, i!=0 now has TRUE or FALSE depending on the value of i.

How do you declare a Boolean variable with a true value?

Syntax: bool b1 = true; // declaring a boolean variable with true value. In C++, the data type bool has been introduced to hold a boolean value, true or false .The values true or false have been added as keywords in the C++ language. Important Points: The default numeric value of true is 1 and false is 0.

How to use bool type variables in mathematical expressions?

We can use bool type variables or values true and false in mathematical expressions also.For instance, int x = false + true + 6; is valid and the expression on right will evaluate to 7 as false has value 0 and true will have value 1.


1 Answers

So, I want to get a bool type. (Actually I wanted to get a bool is true or false record)

You can write a method like this:

public bool GetBooleanValue(string sql)
{
    return the_connection.Query<bool>(sql).FirstOrDefault();
}

The beauty about the FirstOrDefault is that when your query returns an empty row, Dapper will give you false. That suggested code will work as long as your query returns a value that can be translated into a boolean by your data provider. In case of SQL Server you would get:

  • TRUE for GetBooleanValue("select 1");
  • FALSE for GetBooleanValue("select 0");

where 1 and 0 are values from a table column of boolean type.

You can even use the code if you want to test if something exists or a group of values exists something like GetBooleanValue("select COUNT(*) from the_table where the_column='some_filter'").

like image 87
von v. Avatar answered Sep 21 '22 09:09

von v.