0

我正在寻找两个数组之间的缺失事件:$tc_records& $RelationshipEvents_array。两个数组应该有相同的两个条目。

A->B 和反向 B->A 的条目。当我使用-and 语句评估RelationshipEvents 时,是否评估一个(相同)事件,或者-and 的每一侧加载数组并独立评估。

foreach ($record in $tc_records) {
    # is this using the same RelationshipEvent for both comparisons or two different comparisons?
    if($RelationshipEvents_array.entity1Id -eq $record.entity1Id -and $RelationshipEvents_array.entity2Id -eq $record.entity2Id){
        $re_tc_matched_record.Add($record)
    } else {
        $re_tc_not_matched_record.Add($record)
    }    
}
in case this makes any difference:
Name                           Value
----                           -----
PSVersion                      6.2.3
PSEdition                      Core
GitCommitId                    6.2.3
OS                             Darwin 19.0.0 Darwin Kernel Version 19.0.0: Thu Oct 17 16:17:15 PDT 2019; root:xnu-6153.41.3~29/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
4

2 回答 2

1

看起来您的两个数组包含具有各种属性的相似对象。当您执行此操作时:

foreach ($record in $tc_records) {
    if($RelationshipEvents_array.entity1Id -eq $record.entity1Id -and $RelationshipEvents_array.entity2Id -eq $record.entity2Id)
...

您实际上是在将包含所有entity1Id属性的数组(通过从数组中的每个对象中获取该名称的属性来计算$RelationshipEvents_array)与entity1Id当前$record对象的属性进行比较。

除非我误解了您的问题,否则鉴于不同的对象类型,这不太可能评估为 true。

在进行这样的比较时,我通常会做如下的事情来获得匹配:

if($RelationshipEvents_array.entity1Id -contains $record.entity1Id)
...

但是,由于您尝试匹配两个属性,因此最简单的方法可能是:

为您在第二个 foreach 循环中检查的每条记录迭代数组

foreach ($record in $tc_records) {
    foreach ($event in $RelationshipEvents_array) {
        if ($record.entity1Id -eq $event.entity1Id -and $record.entity2Id -eq $event.entity2Id)
{ # Do matchy stuff
...
于 2019-12-10T01:52:37.807 回答
0

您确实有 compare object cmdlet 可以为您进行数组比较,其中有很多选项可以输出数组之间的不匹配对象。

$a1=@(1,2,3,4,5)
$b1=@(1,2,3,4,5,6)

Compare-Object -ReferenceObject $a1 -DifferenceObject $b1

不过,要回答您的问题, -and 只是评估一个单独的第二个函数,该函数也必须等于true要评估的语句。

if (true){#runs}
if (false){#no runs}
if (true -and true){#runs}
if (true -and false){#no runs}
if (false -and true){#no runs}

您似乎-eq在一个单独的数组上使用,没有示例数据很难准确判断,但您应该能够通过简单地执行$RelationshipEvents_array.entity1Id -eq $record.entity1Id并检查它是否返回 true 或 false 来测试您的逻辑,就像您期望的那样。如果$RelationshipEvents_array是您可能想要使用的数组-contains

于 2019-12-10T02:02:53.077 回答