布尔类型返回规则 [英] Bool type return rule
问题描述
我使用精巧的ORM,所以我使用了两个规则Query<T>
& QuerySingle<T>
.查询返回列表QuerySingle返回单个对象.
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).
我的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?
推荐答案
因此,我想获取布尔类型. (实际上,我想得到一个布尔值是真的 或错误的记录)
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();
}
FirstOrDefault
的优点在于,当查询返回空行时,Dapper将为您提供false
.只要查询返回的值可以被数据提供者转换为布尔值,该建议的代码就可以使用.如果使用SQL Server,您将获得:
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");
为TRUE
-
GetBooleanValue("select 0");
为FALSE
- TRUE for
GetBooleanValue("select 1");
- FALSE for
GetBooleanValue("select 0");
其中,其中1
和0
是布尔类型的表列中的值.
where 1
and 0
are values from a table column of boolean type.
如果要测试是否存在某些内容或一组值存在诸如GetBooleanValue("select COUNT(*) from the_table where the_column='some_filter'")
之类的内容,甚至可以使用代码.
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'")
.
这篇关于布尔类型返回规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!