Wid*_*dor 15 c# nullable type-conversion
我有一个数据库查询,它将返回NULL或布尔(位)值.
我希望将此值存储Nullable<bool>在C#中的类型变量中.
我似乎无法找到一个可接受的explict强制转换和转换组合,以简单的方式执行此操作,而不会抛出异常.
它可以用一条可读行完成吗?
编辑:代码请求
private Nullable<bool> IsRestricted;
...//data access
IsRestricted = (bool?)DataBinder.GetPropertyValue(dataObj, "IsRestricted");
Run Code Online (Sandbox Code Playgroud)
也许
IsRestricted = (bool?)(bool)DataBinder.GetPropertyValue(dataObj, "IsRestricted");
Run Code Online (Sandbox Code Playgroud)
gia*_*min 10
假设你有一个datareader博士:
bool? tmp = Convert.IsDBNull(dr["dbnullValue"]) ? null: (bool?) dr["dbnullValue"];
Run Code Online (Sandbox Code Playgroud)
- -添加 - -
或者你可以使用?? 如果您不必检查DBNull但我不确定编译器是否会喜欢这个(我现在无法测试)
bool? tmp = dr["dbnullValue"] ?? (bool?) dr["dbnullValue"];
Run Code Online (Sandbox Code Playgroud)