Performance Powershell使用公共属性合并两个阵列的最快方法?
使用公共属性合并两个数组的最快方法是什么Performance Powershell使用公共属性合并两个阵列的最快方法?,performance,powershell,lookup,Performance,Powershell,Lookup,使用公共属性合并两个数组的最快方法是什么 Users | Select * Username : Joe.Doe Office : Chicago Email : Username : Mike.Smith Office : New York Email : ... 合并应导致: UsersCompleteList | Select * Username : Joe.Doe Office : Chicago Email : jsmith12@example.co
Users | Select *
Username : Joe.Doe
Office : Chicago
Email :
Username : Mike.Smith
Office : New York
Email :
...
合并应导致:
UsersCompleteList | Select *
Username : Joe.Doe
Office : Chicago
Email : jsmith12@example.com
Username : Mike.Smith
Office : New York
Email : mike-smith@example.com
...
类似于
对于每个($users中的用户){($user.Email=$userEmails |?{$.AccountEmail-eq$user.Username})。EmailAddress在大型数据集上花费时间。循环一个集合并将值存储在散列中。然后循环另一个集合并从散列中提取值。类似于:
$hash = @{}
$userEmails | %{ $hash[$_.AccountEmail] = $_.EmailAddress }
$users | %{ $_.Email = $hash[$_.Username] }
如果有其他属性,则只需存储原始对象即可:
$hash = @{}
$userEmails | %{ $hash[$_.AccountEmail] = $_ }
$users | %{
$item = $hash[$_.Username]
$_.Email = $item.EmailAddress
$_.Other = $item.SomethingElse
}
或使用循环代替每个对象的ForEach
,包括:
$hash = @{}
foreach($e in $userEmails) {
$hash[$e.AccountEmail] = $e
}
foreach($u in $users) {
$item = $hash[$u.UserName]
if ($item -ne $null) {
$u.Email = $item.EmailAddress
}
}
循环遍历一个集合并存储在散列中。然后循环遍历另一个。类似于:$hash=@{};userEmails |%{$hash[$\.AccountEmail]=$\\.EmailAddress};$users |%{$\.Email=$hash[$\.Username]}
这是否回答了您的问题?。使用回答中提到的:$Users | Join Object$UserEmails-On UserName-Eq AccountEmail
假设他没有使用cmdlet获取两个数组,那么使用类似foreach($UserEmails中的email){…}的循环语句可能会获得更好的性能
比使用ForEach对象
…除非您的输入和/或输出速度慢(例如磁盘和/或非常大的文件),否则您可能最好从头到尾使用管道:导入Csv。\Users | ForEach对象{…}|导出Csv。\output.Csv
(在这种情况下,您还应该从头到尾比较/衡量这两种解决方案)。
$hash = @{}
foreach($e in $userEmails) {
$hash[$e.AccountEmail] = $e
}
foreach($u in $users) {
$item = $hash[$u.UserName]
if ($item -ne $null) {
$u.Email = $item.EmailAddress
}
}