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?
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:
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.
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.
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.
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:
GetBooleanValue("select 1");
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'")
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With