检查空值并在不存在时分配另一个值的最短方法
我正在从数据库中提取 varchar
值,并希望将 string
设置为“”(如果它们为 null
)。 我目前正在这样做:
if (string.IsNullOrEmpty(planRec.approved_by) == true)
this.approved_by = "";
else
this.approved_by = planRec.approved_by.toString();
似乎应该有一种方法可以在一行中执行此操作,例如:
this.approved_by = "" || planRec.approved_by.toString();
但是我找不到执行此操作的最佳方法。 有更好的方法还是我有最好的方法?
I am pulling varchar
values out of a DB and want to set the string
I am assigning them to as "" if they are null
. I'm currently doing it like this:
if (string.IsNullOrEmpty(planRec.approved_by) == true)
this.approved_by = "";
else
this.approved_by = planRec.approved_by.toString();
There seems like there should be a way to do this in a single line something like:
this.approved_by = "" || planRec.approved_by.toString();
However I can't find an optimal way to do this. Is there a better way or is what I have the best way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
试试这个:
您也可以像其他人所说的那样使用空合并运算符 - 因为没有人给出与您的代码一起使用的示例,这里是一个:
但是这个示例仅在
this.approved_by< 的可能值之后才有效/code> 与您希望将其设置为的潜在值之一相同。 对于所有其他情况,您将需要使用条件运算符,如我在第一个示例中所示。
Try this:
You can also use the null-coalescing operator as other have said - since no one has given an example that works with your code here is one:
But this example only works since a possible value for
this.approved_by
is the same as one of the potential values that you wish to set it to. For all other cases you will need to use the conditional operator as I showed in my first example.从 C# 8.0 开始,您可以使用
??=
运算符将表单的代码替换为以下代码:
更多信息位于 此处
Starting with C# 8.0, you can use the
??=
operator to replace the code of the formwith the following code:
More information is here
您正在寻找 C# 合并运算符:??。 该运算符采用左参数和右参数。 如果运算符的左侧为 null 或可以为 null 且没有值,则它将返回右侧参数。 否则它将返回左侧。
You are looking for the C# coalesce operator: ??. This operator takes a left and right argument. If the left hand side of the operator is null or a nullable with no value it will return the right argument. Otherwise it will return the left.
我相信 合并运算符 (??) 就是您想要的。
The coalesce operator (??) is what you want, I believe.
我的猜测是你能想到的最好的办法是
当然,因为你暗示
approved_by
是一个object
(不能等于“”),所以这会被重写为My guess is the best you can come up with is
Of course since you're hinting at the fact that
approved_by
is anobject
(which cannot equal ""), this would be rewritten as对于 C#6,对于 planRec.approved_by 不是字符串的情况,有一种稍微更短的方法:
With C#6 there is a slightly shorter way for the case where planRec.approved_by is not a string:
使用 C# 合并运算符:??
Use the C# coalesce operator: ??
扩展 @Dave 的回答...如果 planRec.approved_by 已经是一个字符串
To extend @Dave's answer...if planRec.approved_by is already a string
要分配一个非空变量而不重复实际的变量名称(并且如果变量为空则不分配任何内容!),您可以使用带有
Action
参数的小辅助方法:然后只需使用它:
To assign a non-empty variable without repeating the actual variable name (and without assigning anything if variable is null!), you can use a little helper method with a
Action
parameter:And then just use it:
所接受的答案在给出时是及时正确的。
对于仍然发现这个问题的人:
今天您可以使用 ??= 运算符。
例如:
The accepted answer was correct in time, when it was given.
For people still finding this question:
Today you can use the ??= Operator.
e.g:
您也可以在查询中执行此操作,例如在 sql server、google
ISNULL
和CASE
内置函数中。You can also do it in your query, for instance in sql server, google
ISNULL
andCASE
built-in functions.我使用扩展方法 SelfChk
然后使用 like
I use extention method SelfChk
Then use like