在Powershell中测试集合中的所有元素是否具有相同的属性值
有没有更简洁/惯用的方法来完成以下任务?我对逻辑中的双重否定印象不深 e、 g.对于集合中的所有项目,在Powershell中测试集合中的所有元素是否具有相同的属性值,powershell,Powershell,有没有更简洁/惯用的方法来完成以下任务?我对逻辑中的双重否定印象不深 e、 g.对于集合中的所有项目,IsPass为true $data = ,@{'IsPass' = $true}, @{'IsPass' = $true}, @{'IsPass' = $true} if(!$data.IsPass.Contains($false)) { Write-Host 'foo' } 你的方法到底有什么问题?我能想到这样的事情: $data = @{'IsPass' = $true}, @{'
IsPass
为true
$data = ,@{'IsPass' = $true}, @{'IsPass' = $true}, @{'IsPass' = $true}
if(!$data.IsPass.Contains($false))
{
Write-Host 'foo'
}
你的方法到底有什么问题?我能想到这样的事情:
$data = @{'IsPass' = $true}, @{'IsPass' = $true}, @{'IsPass' = $true}
foreach ($item in $data)
{
if ($item['IsPass'] -ne $true)
{
Write-Host 'foo'
}
}
或者这个:
$data = @{'IsPass' = $true}, @{'IsPass' = $false}, @{'IsPass' = $true}
$data.IsPass | Where-Object {$_ -ne $true}
逻辑上有双重否定。i、 e.“集合中是否没有false”,奇怪的逻辑在我看来。在c#中,这只是
collection.All(d=>d.IsPass)
,这(imo)更容易阅读。此外,您的代码的行为与示例不一样。它将输出不匹配的每个元素,而不是任何元素。