相关疑难解决方法(0)

?? 合并空字符串?

我发现自己做的越来越多的事情是检查字符串是否为空(如in ""或null)和条件运算符.

一个当前的例子:

s.SiteNumber.IsNullOrEmpty() ? "No Number" : s.SiteNumber;
Run Code Online (Sandbox Code Playgroud)

这只是一种扩展方法,它相当于:

string.IsNullOrEmpty(s.SiteNumber) ? "No Number" : s.SiteNumber;
Run Code Online (Sandbox Code Playgroud)

因为它是空的而不是null,所以??不会这样做.一个string.IsNullOrEmpty()版本??将是完美的解决方案.我认为必须有一个更清洁的方式来做这件事(我希望!),但我一直在找不到它.

有没有人知道更好的方法来做到这一点,即使它只在.Net 4.0中?

c# string coalesce null-coalescing-operator

154
推荐指数
7
解决办法
6万
查看次数

在powershell中无法合并

在PowerShell中是否存在空合并运算符?

我希望能够在powershell中执行这些c#命令:

var s = myval ?? "new value";
var x = myval == null ? "" : otherval;
Run Code Online (Sandbox Code Playgroud)

powershell null-coalescing-operator

97
推荐指数
6
解决办法
4万
查看次数

检查字符串是不是NULL还是EMPTY

在下面的代码中,我需要检查版本字符串是否为空,然后将其值附加到请求变量.

if ([string]::IsNullOrEmpty($version))
{
    $request += "/" + $version
}
Run Code Online (Sandbox Code Playgroud)

如果有条件,如何检查?

powershell

36
推荐指数
4
解决办法
14万
查看次数

为什么[NullString] :: Value使用断点进行不同的求值?

我在PowerShell ISE和VS Code中尝试了这个代码,结果相同.没有断点,输出是EMPTY,但是在行中有断点"NULL",输出是NULL(如预期的那样).为什么?

function demo {
    param(
        [string] $value = [NullString]::Value
    )

    if ($null -eq $value) {
        "NULL"
    } elseif ($value -eq '') {
        "EMPTY"
    } else {
        "$value"
    }
}

demo
Run Code Online (Sandbox Code Playgroud)

我现在知道,当你对参数使用类型修饰符[string]时,PowerShell总是将非字符串值(例如$ null或[NullString] :: Value)转换为(空)字符串.好吧,我可以忍受这一点,但是如果调试在这种情况下如此奇怪,那么很难自己解决这个问题.

debugging powershell breakpoints null-string

5
推荐指数
1
解决办法
165
查看次数

Why do PowerShell comparison operators not enumerate collections of size 1?

When checking variables and collections of variables for nullity, comparison operators seem to enumerate collections of size 2 or more:

> if ( @( $null, $null ) -eq $null ) { $True } else { $False }
True
Run Code Online (Sandbox Code Playgroud)

But they do not for collections of size 1:

> if ( @( $null ) -eq $null ) { $True } else { $False }
False
Run Code Online (Sandbox Code Playgroud)

I'm aware that it's best practice to null-compare using the left-hand side ($null -eq @( …

powershell

4
推荐指数
2
解决办法
153
查看次数

PowerShellWhere-Object 查询括号之间的空或 NULL 内容

如果我的问题没有完全描述我在这里要问的内容,我很抱歉!我不确定如何过滤大括号/括号之间的字段内容或缺失。我的热门搜索结果返回这篇文章,我认为它不能解决我的问题。

我正在尝试从 MSOnline/AzureAD 中提取禁用/未设置多重身份验证选项的所有用户。这个事实有点微不足道,但它设定了背景......

我有以下查询,它返回一个我知道没有配置 StrongAuthenticationRequirements 值的测试用户。

> 获取-MsolUser -UserPrincipalName test@contoso.com | 选择 UserPrincipalName、DisplayName、StrongAuthenticationRequirements

UserPrincipalName DisplayName StrongAuthenticationRequirements
----------------- ---------------------- ---------------------- ----------
test@contoso.com 测试帐户 {}

我还可以使用Where-Object 运行查询来查找那些确实设置了值的对象。

> 获取-MsolUser | where-object { $_.StrongAuthenticationRequirements -like "*Mi​​crosoft.Online.Administration.StrongAuthenticationRequirement*" } | 选择 UserPrincipalName、DisplayName、StrongAuthenticationRequirements

UserPrincipalName DisplayName StrongAuthenticationRequirements
----------------- ---------------------- ---------------------- ----------
test@contoso.com 测试帐户 {Microsoft.Online.Administration.StrongAuthenticationRequirement}

我的问题是;如何运行非用户特定查询来查找 StrongAuthenticationRequirement 字段为“{}”的所有实例?

提前致谢!

powershell filtering

0
推荐指数
1
解决办法
1万
查看次数