检查空值并在不存在时分配另一个值的最短方法

发布于 2024-07-27 13:28:42 字数 434 浏览 5 评论 0原文

我正在从数据库中提取 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 技术交流群。

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

发布评论

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

评论(12

匿名的好友 2024-08-03 13:28:42

试试这个:

this.approved_by = IsNullOrEmpty(planRec.approved_by) ? string.Empty : planRec.approved_by.toString();

您也可以像其他人所说的那样使用空合并运算符 - 因为没有人给出与您的代码一起使用的示例,这里是一个:

this.approved_by = planRec.approved_by ?? planRec.approved_by.toString();

但是这个示例仅在 this.approved_by< 的可能值之后才有效/code> 与您希望将其设置为的潜在值之一相同。 对于所有其他情况,您将需要使用条件运算符,如我在第一个示例中所示。

Try this:

this.approved_by = IsNullOrEmpty(planRec.approved_by) ? string.Empty : planRec.approved_by.toString();

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:

this.approved_by = planRec.approved_by ?? planRec.approved_by.toString();

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.

流心雨 2024-08-03 13:28:42

从 C# 8.0 开始,您可以使用 ??= 运算符将表单的代码替换

if (variable is null)
{
    variable = expression;
}

为以下代码:

variable ??= expression;

更多信息位于 此处

Starting with C# 8.0, you can use the ??= operator to replace the code of the form

if (variable is null)
{
    variable = expression;
}

with the following code:

variable ??= expression;

More information is here

宛菡 2024-08-03 13:28:42

您正在寻找 C# 合并运算符:??。 该运算符采用左参数和右参数。 如果运算符的左侧为 null 或可以为 null 且没有值,则它将返回右侧参数。 否则它将返回左侧。

var x = somePossiblyNullValue ?? valueIfNull;

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.

var x = somePossiblyNullValue ?? valueIfNull;
以歌曲疗慰 2024-08-03 13:28:42

我相信 合并运算符 (??) 就是您想要的。

The coalesce operator (??) is what you want, I believe.

始于初秋 2024-08-03 13:28:42

我的猜测是你能想到的最好的办法是

this.approved_by = IsNullOrEmpty(planRec.approved_by) ? string.Empty
                                                      : planRec.approved_by.ToString();

当然,因为你暗示 approved_by 是一个 object (不能等于“”),所以这会被重写为

this.approved_by = (planRec.approved_by ?? string.Empty).ToString();

My guess is the best you can come up with is

this.approved_by = IsNullOrEmpty(planRec.approved_by) ? string.Empty
                                                      : planRec.approved_by.ToString();

Of course since you're hinting at the fact that approved_by is an object (which cannot equal ""), this would be rewritten as

this.approved_by = (planRec.approved_by ?? string.Empty).ToString();
樱花坊 2024-08-03 13:28:42

对于 C#6,对于 planRec.approved_by 不是字符串的情况,有一种稍微更短的方法:

this.approved_by = planRec.approved_by?.ToString() ?? "";

With C#6 there is a slightly shorter way for the case where planRec.approved_by is not a string:

this.approved_by = planRec.approved_by?.ToString() ?? "";
桃酥萝莉 2024-08-03 13:28:42

使用 C# 合并运算符:??

// if Value is not null, newValue = Value else if Value is null newValue is YournullValue
var newValue = Value ?? YourNullReplacement;

Use the C# coalesce operator: ??

// if Value is not null, newValue = Value else if Value is null newValue is YournullValue
var newValue = Value ?? YourNullReplacement;
月竹挽风 2024-08-03 13:28:42

扩展 @Dave 的回答...如果 planRec.approved_by 已经是一个字符串

this.approved_by = planRec.approved_by ?? "";

To extend @Dave's answer...if planRec.approved_by is already a string

this.approved_by = planRec.approved_by ?? "";
裸钻 2024-08-03 13:28:42

要分配一个非空变量而不重复实际的变量名称(并且如果变量为空则不分配任何内容!),您可以使用带有 Action 参数的小辅助方法:

public static void CallIfNonEmpty(string value, Action<string> action)
{
    if (!string.IsNullOrEmpty(value))
        action(value);
}

然后只需使用它:

CallIfNonEmpty(this.approved_by, (s) => planRec.approved_by = s);

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:

public static void CallIfNonEmpty(string value, Action<string> action)
{
    if (!string.IsNullOrEmpty(value))
        action(value);
}

And then just use it:

CallIfNonEmpty(this.approved_by, (s) => planRec.approved_by = s);
入怼 2024-08-03 13:28:42

所接受的答案在给出时是及时正确的。

对于仍然发现这个问题的人:
今天您可以使用 ??= 运算符。

例如:

private string _test = null;

private void InitIfNull(){
 _test ??= "Init";
}

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:

private string _test = null;

private void InitIfNull(){
 _test ??= "Init";
}
九局 2024-08-03 13:28:42

您也可以在查询中执行此操作,例如在 sql server、google ISNULLCASE 内置函数中。

You can also do it in your query, for instance in sql server, google ISNULL and CASE built-in functions.

2024-08-03 13:28:42

我使用扩展方法 SelfChk

static class MyExt {
//Self Check 
 public static void SC(this string you,ref string me)
    {
        me = me ?? you;
    }
}

然后使用 like

string a = null;
"A".SC(ref a);

I use extention method SelfChk

static class MyExt {
//Self Check 
 public static void SC(this string you,ref string me)
    {
        me = me ?? you;
    }
}

Then use like

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