比较对象未返回预期行

问题描述 投票:0回答:1

我正在使用 Compare-Object 来比较两个数组。简化版本是:

$dataFields :git 存储库中数据模型中所有字段的列表。

Dataset   Table             Field
-------   -----             -----
Sales     BusinessUnit      BusinessUnit

$reportFields:生产中的报告中使用的所有字段的列表。

Workspace       Report              Dataset   Page                    Object       Type      Table             Field             
---------       ------              -------   ----                    ------       ----      -----             -----             
Sales           Invoices            Sales     Analysis                             Rows      BusinessUnit      BusinessUnit 
Sales           Customers           Sales     ReportLevel             ReportFilter Filter    BusinessUnit      BusinessUnit 
Sales           Customers           Sales     Customer Insight                     Category  BusinessUnit      BusinessUnit 
Sales           Customers           Sales     Customer Analysis                    ExplainBy BusinessUnit      BusinessUnit 
Sales           Revenue             Sales     ReportLevel             ReportFilter Filter    BusinessUnit      BusinessUnit 
Sales           Revenue             Sales     Analysis                             Rows      BusinessUnit      BusinessUnit 
NewServices     Projects            Sales     ReportLevel             ReportFilter Filter    BusinessUnit      BusinessUnit 

重点是比较它们,看看从我们的数据模型中删除字段是否会影响任何报告。

但是,当我跑步时:

$compareObjects = @{
    ReferenceObject  = $reportFields 
    DifferenceObject = $dataFields 
}

Compare-Object @compareObjects -Property Dataset, Table, Field -IncludeEqual -PassThru | Format-Table

我得到下面的结果。请注意,只有 $reportFields 中的第一行是匹配的,而我希望在此示例中每一行都匹配。

Workspace       Report              Dataset   Page                    Object       Type      Table             Field        SideIndicator      
---------       ------              -------   ----                    ------       ----      -----             -----        -----           
Sales           Invoices            Sales     Analysis                             Rows      BusinessUnit      BusinessUnit == 
Sales           Customers           Sales     ReportLevel             ReportFilter Filter    BusinessUnit      BusinessUnit =>
Sales           Customers           Sales     Customer Insight                     Category  BusinessUnit      BusinessUnit =>
Sales           Customers           Sales     Customer Analysis                    ExplainBy BusinessUnit      BusinessUnit =>
Sales           Revenue             Sales     ReportLevel             ReportFilter Filter    BusinessUnit      BusinessUnit =>
Sales           Revenue             Sales     Analysis                             Rows      BusinessUnit      BusinessUnit =>
NewServices     Projects            Sales     ReportLevel             ReportFilter Filter    BusinessUnit      BusinessUnit =>
powershell
1个回答
0
投票

正如Santiago在评论中提到的,报告字段中的对象比数据字段中的对象多。

Compare-Object
一对一匹配对象,因此您只会得到一个显示“==”的报告字段,因为数据字段中只有 1 个对象可以匹配。

要自行测试比较,一个相对有效的方法可以是使用哈希表

# @() will output all as a array.  I start with report fields on purpose 
# to ensure that all of the properties are shown in the resulting table 
# since format table will show only the properties available in the first object
@(
    # use GetEnumerator and ForEach-Object to iterate through all the 
    # key-value pairs  of both hashtables to ensure that a row from 
    # each colletion is in the output
    # (key = concatenated properties, value = collection of grouped objects)
    $reportFields_hash.GetEnumerator() | ForEach-Object {
        # optionally add a field to indicate which collection 
        # the object is a part of (report vs data)
        $_.Value | Add-Member -NotePropertyName 'FieldType' -NotePropertyValue 'Report Field'
        if ($_.Key -in $dataFields_hash.Keys) {
            # check if key is also found in data field hashtable.  If so then equal
            $_.Value | Add-Member -NotePropertyName 'SideIndicator' -NotePropertyValue '==' -PassThru
        }
        else {
            # otherwise we will indicate LHS
            $_.Value | Add-Member -NotePropertyName 'SideIndicator' -NotePropertyValue '<=' -PassThru
        }
    }

    $dataFields_hash.GetEnumerator() | ForEach-Object {
        # optionally add a field to indicate which collection 
        # the object is a part of (report vs data)
        $_.Value | Add-Member -NotePropertyName 'FieldType' -NotePropertyValue 'Data Field'
        if ($_.Key -in $reportFields_hash.Keys) {
            # check if key is also found in report field hashtable.  If so then equal
            $_.Value | Add-Member -NotePropertyName 'SideIndicator' -NotePropertyValue '==' -PassThru
        }
        else {
            # otherwise we will indicate RHS
            $_.Value | Add-Member -NotePropertyName 'SideIndicator' -NotePropertyValue '=>' -PassThru
        }
    }
) | Format-Table

输出

workspace   report    dataset  page              object       type     table        field        FieldType    SideIndicator
---------   ------    -------  ----              ------       ----     -----        -----        ---------    -------------
other       projects  products                                         product      client       Report Field <=
other       projects  sales                                            client       client       Report Field <=
sales       invoices  sales    analysis                       rows     businessunit businessunit Report Field ==
sales       customers sales    reportlevel       reportfilter filter   businessunit businessunit Report Field ==
sales       customers sales    customer insight               category businessunit businessunit Report Field ==
sales       customers sales    customer analysis              explain  businessunit businessunit Report Field ==
sales       revenue   sales    reportlevel       reportfilter filter   businessunit businessunit Report Field ==
sales       revenue   sales    analysis                       rows     businessunit businessunit Report Field ==
newservices projects  sales    reportlevel                    filter   businessunit businessunit Report Field ==
                      sales                                            businessunit businessunit Data Field   ==
                      notsales                                         client       client       Data Field   =>

© www.soinside.com 2019 - 2024. All rights reserved.