?? system.DBNull 中的运算符

发布于 2024-11-16 20:30:41 字数 1458 浏览 2 评论 0原文

是否有一个运算符或内置函数来简化这一点:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-11-23 20:30:41

怎么样:

public static class StringHelper {
    public static string ValueOrDash(this string value) {
        return string.IsNullOrEmpty(value) ? "-" : value;
    }
}

那么你可以这样做:

myVal = object1.object2.something(a,b).DataColumn.ToString().ValueOrDash();

或者更好:

public static class StringHelper {
    public static string ValueOrWhatever(this string value, string defaultValue) {
        return string.IsNullOrEmpty(value) ? defaultValue : value;
    }
}

myVal = object1.object2.something(a,b).DataColumn.ToString().ValueOrWhatever("-");

How about:

public static class StringHelper {
    public static string ValueOrDash(this string value) {
        return string.IsNullOrEmpty(value) ? "-" : value;
    }
}

Then you can just do:

myVal = object1.object2.something(a,b).DataColumn.ToString().ValueOrDash();

Or better yet:

public static class StringHelper {
    public static string ValueOrWhatever(this string value, string defaultValue) {
        return string.IsNullOrEmpty(value) ? defaultValue : value;
    }
}

myVal = object1.object2.something(a,b).DataColumn.ToString().ValueOrWhatever("-");
棒棒糖 2024-11-23 20:30:41

如果底层对象始终是字符串或 DbNull,则可以使用 as

private string CheckNull(object dataColumn){
    return dataColumn == (dataColumn as string)??"-":dataColumn;
}

强制转换运算符:如果值是整数,例如或其他一些类型。

从您的代码中尚不清楚,但如果这些值来自 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:

private string CheckNull(object dataColumn){
    return dataColumn == (dataColumn as string)??"-":dataColumn;
}

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 return null instead of DbNull.

最初的梦 2024-11-23 20:30:41

您可以编写一个扩展方法来调用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文