?? system.DBNull 中的运算符
是否有一个运算符或内置函数来简化这一点:
myVal = object1.object2.something(a,b).dataColumn.toString()==""?object1.object2.something(a,b).dataColumn.toString():"-";
我知道我可以做类似的事情:
string str = object1.object2.something(a,b).dataColumn.toString();
myVal =str==""?"-":str;
但我有很多对象,我想避免它:
string str1 = object1.object2.something(a,b).dataColumn1.toString();
myVal1==""?str1:"-"
string str2 = object1.object2.something(a,b).dataColumn2.toString();
myVal2==""?str2:"-"
:
string strN = object1.object2.something(a,b).dataColumnN.toString();
myValN==""?strN:"-"
我还可以创建一个函数:
private string CheckNull(object dataColumn){
return dataColumn == System.DBNull?"-":dataColumn.toString();
}
myVal1 = CheckNull(object1.object2.something1(a,b).dataColumn.toString())
myVal2 = CheckNull(object1.object2.something2(a,b).dataColumn.toString())
myVal3 = CheckNull(object1.object2.something3(a,b).dataColumn.toString())
最简单的方法是使用 ??运算符,但问题是“dataColumn”与 ?? 不兼容因为有时会返回 system.DBNull 而不是 null。查看立即窗口输出:
System.DBNull??"-"
'System.DBNull' is a 'type', which is not valid in the given context
null??"-"
"-"
我想知道是否有一些(字符串)函数或运算符可以在没有 if..then 的情况下返回“-” if dataColumn.toString()=="" (因为我必须做很多 if ..然后对于所有这些,我宁愿使用上面所示的函数方法。
string str = object1.object2.something(a,b).dataColumn.toString().if("","-");
Is there an operator or built in function to simplyfy this:
myVal = object1.object2.something(a,b).dataColumn.toString()==""?object1.object2.something(a,b).dataColumn.toString():"-";
I know i can do something like:
string str = object1.object2.something(a,b).dataColumn.toString();
myVal =str==""?"-":str;
but I have many objects and I want to avoid it:
string str1 = object1.object2.something(a,b).dataColumn1.toString();
myVal1==""?str1:"-"
string str2 = object1.object2.something(a,b).dataColumn2.toString();
myVal2==""?str2:"-"
:
string strN = object1.object2.something(a,b).dataColumnN.toString();
myValN==""?strN:"-"
I can also create a function:
private string CheckNull(object dataColumn){
return dataColumn == System.DBNull?"-":dataColumn.toString();
}
myVal1 = CheckNull(object1.object2.something1(a,b).dataColumn.toString())
myVal2 = CheckNull(object1.object2.something2(a,b).dataColumn.toString())
myVal3 = CheckNull(object1.object2.something3(a,b).dataColumn.toString())
The easiest way is to use the ?? operator but the problem is that 'dataColumn' is not compatible with ?? because sometimes returns a system.DBNull instead of a null. Look at the immediate window output:
System.DBNull??"-"
'System.DBNull' is a 'type', which is not valid in the given context
null??"-"
"-"
I wonder if there is some (string) function or operator that can return "-" if dataColumn.toString()=="" without an if..then (because I would have to make many if..then for all of them. Otherwise I rather use the function approach shown above.
string str = object1.object2.something(a,b).dataColumn.toString().if("","-");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
那么你可以这样做:
或者更好:
How about:
Then you can just do:
Or better yet:
如果底层对象始终是字符串或 DbNull,则可以使用
as
强制转换运算符:如果值是整数,例如或其他一些类型。
从您的代码中尚不清楚,但如果这些值来自 DataRows,您可以使用新的 (.NET 3.5)
DataRowExtensions.Field
函数,它将返回null
而不是DbNull
。If the underlying object is always either a string or DbNull, you could use the
as
cast operator:This will also return "-" if the value is an integer for example or some other type.
It's not clear from your code, but if these values are coming from DataRows you could use the new (.NET 3.5)
DataRowExtensions.Field
function, which will returnnull
instead ofDbNull
.您可以编写一个扩展方法来调用 dataColumn。不过,它在功能上与您的 CheckNull 方法没有太大不同。
或者在第一个包含 str1、str2、strN 等的示例中,是否有理由不能对每个示例重复使用 str1?我看到你的长行代码中有一个方法调用,你不想浪费时间运行比你需要的更多的时间(特别是如果它每次都会产生相同的输出)。
You could write an extension method to call on the dataColumn. It wouldn't functionally be much different than your CheckNull method, though.
Or in your first example with str1, str2, strN and such, is there a reason you can't reuse str1 for each? I see you have a method call in your long line of code, you wouldn't want to waste the time running that more often than you need to (especially if it's going to result in the same output every time).