Select * Username : Joe.Doe Office : Chicago Email : Username : Mike.Smith Office : New York Email : ... ...

问题描述 投票:3回答:1
Loop through one collection and store the values in a hash. Then loop through the other collection and pull the value back out of the hash. Something like:

If you have other properties you can just store the original object:

Users | Select *
Username : Joe.Doe
Office   : Chicago
Email    :

Username : Mike.Smith
Office   : New York
Email    :
...
UserEmails | Select *
AccountEmail  : Mike.Smith
EmailAddress  : [email protected]

AccountEmail  : Joe.Doe
EmailAddress  : [email protected]
...

Or with loops instead of

UsersCompleteList | Select *
Username : Joe.Doe
Office   : Chicago
Email    : [email protected]

Username : Mike.Smith
Office   : New York
Email    : [email protected]
...

including:for each ($user in $users) { ($user.Email = $userEmails | ? { $_.AccountEmail -eq $user.Username}).EmailAddress

使用公共属性合并两个数组的最快方法是什么?
performance powershell lookup
1个回答
5
投票

在大数据集上需要花费很长时间。

$hash = @{}
$userEmails | %{ $hash[$_.AccountEmail] = $_.EmailAddress }
$users | %{ $_.Email = $hash[$_.Username] }

$hash = @{}
$userEmails | %{ $hash[$_.AccountEmail] = $_ }
$users | %{ 
   $item = $hash[$_.Username]
   $_.Email = $item.EmailAddress
   $_.Other = $item.SomethingElse
}

ForEach-Object使用公共属性合并两个数组的最快方法是什么?用户

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