1

说[假设地],我有两个 .CSV 我正在比较尝试看看我当前的哪些成员是原始成员......我写了一个嵌套的 ForEach-Object 将每个对象中的每个 $name 和 $memberNumber 与每个其他对象进行比较. 它工作得很好,但需要很长时间,特别是因为每个 CSV 都有成千上万个对象。我还有其他方法可以解决这个问题吗?

Original_Members.csv

姓名、会员号码

爱丽丝,1234

吉姆, 4567

Current_Members.csv

爱丽丝,4599

吉姆,4567

$currentMembers = import-csv $home\Desktop\current_members.csv |

ForEach-Object {
    $name = $_.Name      
    $memNum = $_."Member Number"

    $ogMembers = import-csv $home\Desktop\original_members.csv" |
        ForEach-Object {
            If ($ogMembers.Name -eq $name -and $ogMembers."Member Number" -eq $memNum) {
                $ogMember = "Yes"
            }  
            Else {
                $ogMember = "No"
            }
        }
            [pscustomobject]@{
            "Name"=$name
            "Member Number"=$memNum
            "Original Member?"=$ogMember
            }
} |

select "Name","Member Number","Original Member?" |
Export-CSV "$home\Desktop\OG_Compare_$(get-date -uformat "%d%b%Y").csv" -Append -NoTypeInformation
4

1 回答 1

1

假设您的两个文件都如下所示:

Original_Members.csv

Name, Member_Number
Alice, 1234
Jim, 4567

Current_Members.csv

Name, Member_Number
Alice, 4599
Jim, 4567

您可以将原始成员名称存储在System.Collections.Generic.HashSet<T>恒定时间查找中,而不是对每个名称进行线性搜索。我们可以使用它System.Linq.Enumerable.ToHashSet来创建string[]名称的哈希集。

然后我们可以Where-Object通过检查哈希集是否包含原始名称来过滤当前名称System.Collections.Generic.HashSet<T>.Contains(T),这是一个 O(1) 方法。

$originalMembers = Import-Csv -Path .\Original_Members.csv
$currentMembers = Import-Csv -Path .\Current_Members.csv

$originalMembersLookup = [Linq.Enumerable]::ToHashSet(
    [string[]]$originalMembers.Name, 
    [StringComparer]::CurrentCultureIgnoreCase
)

$currentMembers | 
    Where-Object {$originalMembersLookup.Contains($_.Name)}

这将输出作为原始成员的当前成员:

Name  Member_Number
----  -------------
Alice 4599
Jim   4567

更新

根据评论中的要求,如果我们想同时检查NameMember_Number,我们可以连接两个字符串以用于查找:

$originalMembers = Import-Csv -Path .\Original_Members.csv
$currentMembers = Import-Csv -Path .\Current_Members.csv

$originalMembersLookup = [Linq.Enumerable]::ToHashSet(
    [string[]]($originalMembers | 
        ForEach-Object {
            $_.Name + $_.Member_Number
        }), 
    [StringComparer]::CurrentCultureIgnoreCase
)

$currentMembers | 
    Where-Object {$originalMembersLookup.Contains($_.Name + $_.Member_Number)}

现在只会返回:

Name Member_Number
---- -------------
Jim  4567
于 2020-05-01T18:56:40.550 回答