Object 对象值之间的PowerShell关系

Object 对象值之间的PowerShell关系,object,powershell,match,Object,Powershell,Match,这似乎是一个非常愚蠢的问题,但我似乎无法理解。如何在对象的值之间建立关系 我认为该准则本身就是明证: $Collection = ("Fruit","Banana"),("Vegetables","Aubergine"),("Fruit","Appel"),("Vegetables","Courgette") | ForEach-Object { [PSCustomObject]@{ 'Type'=$_[0] 'Conte

这似乎是一个非常愚蠢的问题,但我似乎无法理解。如何在对象的值之间建立关系

我认为该准则本身就是明证:

$Collection = ("Fruit","Banana"),("Vegetables","Aubergine"),("Fruit","Appel"),("Vegetables","Courgette") | 
    ForEach-Object {
        [PSCustomObject]@{
            'Type'=$_[0]
            'Content'=$_[1]
        }
    }    
if ($Collection.Type -contains "Fruit" -and $Collection.Content -contains "Courgette") {
    Write-Host "We DID find a match" -ForegroundColor Green
}
else {
    Write-Host "We didn't find a match" -ForegroundColor Red
}

<# Result
Fruit + Banana = "We DID find a match"
Fruit + Courgette= "We DID find a match"
#>
$Collection=(“水果”、“香蕉”)、(“蔬菜”、“茄子”)、(“水果”、“Appel”)、(“蔬菜”、“小胡瓜”)|
ForEach对象{
[PSCustomObject]@{
“类型”=$\u0]
“内容”=$\uU1]
}
}    
如果($Collection.Type-包含“水果”-和$Collection.Content-包含“小胡瓜”){
写下主持人“我们确实找到了匹配的人”-背景颜色为绿色
}
否则{
写下主持人“我们没有找到匹配的”-前底色为红色
}
怎么可能只有一对匹配时才有匹配?例如,我希望在
水果+香蕉
的情况下匹配,但在
水果+小胡瓜
的情况下不匹配


如果这听起来很傻,我很抱歉。。感谢您的帮助。

您的收藏具有唯一的价值(内容)和可重复的类型。在我的解决方案中,我假设您永远不会重复输入“水果/香蕉”和“蔬菜/香蕉”

您的逻辑测试是:
$Collection.Type-包含“水果”-而$Collection.Content-包含“小胡瓜”

你的逻辑测试有什么问题?您正在测试您的收藏是否有“水果”(这是真的)以及您的收藏是否有“Courguette”(这也是真的)

您必须测试您的收藏是否有“小胡瓜”,如果是,那么“小胡瓜”对象的类型是否为“水果”

您可以创建一个函数来查找$content,如果它存在,则检查类型。匹配时它应该返回$true

function FindObject($type, $content)
    {
        $index = [array]::IndexOf($Collection.Content,$content)

        if (($index -ge 0) -and ($Collection[$index].Type -eq $type))
        {
            return $true
        }
        else
        {
            return $false
        }

    }
编辑:正如@Kayasax评论的那样,您可以使用
$Collection |?{$\.Type-eq“Fruit”-和$\.Content-eq“Banana”}
而不是函数。如果您想理解,代码行更少,但更难阅读和调试

工作示例:

$Collection = ("Fruit","Banana"),("Vegetables","Aubergine"),("Fruit","Appel"),("Vegetables","Courgette") | 
    ForEach-Object {
        [PSCustomObject]@{
            'Type'=$_[0]
            'Content'=$_[1]
        }
    }    

function FindObject($type, $content)
{
    $index = [array]::IndexOf($Collection.Content,$content)

    if (($index -ge 0) -and ($Collection[$index].Type -eq $type))
    {
        return $true
    }
    else
    {
        return $false
    }

}

#if ( $Collection | ?{$_.Type -eq "Fruit" -and $_.Content -eq "Courgette"} )
if ( (FindObject "Fruit" "Courgette") -eq $true)
{
    Write-Host "We DID find a match" -ForegroundColor Green
}
else 
{
    Write-Host "We didn't find a match" -ForegroundColor Red
}

您不必创建新的自定义对象,PS已经以哈希表的形式提供了键/值集合。您可以让这变得更简单:

$h=@{“香蕉”=“水果”;“茄子”=“蔬菜”;“Appel”=“水果”;“小胡瓜”=“蔬菜”}
函数CheckObject($k,$v){
如果($h.项目($k)-等式$v){
写主机“我们确实找到了匹配项”;
}
否则{
写主机“我们没有找到匹配项”;
}
}
结果:

PS U:\>检查对象“香蕉”“水果”
我们确实找到了一对
PS U:\>检查对象“香蕉”“蔬菜”
我们没有找到匹配的
PS U:\>选中对象“小胡瓜”“蔬菜”
我们没有找到匹配的

键(第一个值)必须是唯一的,因此从
“Fruit”、“Banana”

$Collection |?{$$\u.Type-eq“Fruit”-和$\u.Content-eq“Banana”}
??