4

我有两个从两个不同 XML 文件的数据创建的哈希表。我想做的是根据两个表中的共同值将两个表组合成一个哈希表。

投资哈希:

    $invHash = $invXML.InventoryDto.ProductInventoryItem.SkuInventoryItem | 
select @{ L = 'SkuID'; E = { $_.SkuId } }, @{ L = 'SkuStatusCode'; 
E = { if ($_.SkuStatusCode -eq 'Active') { 'True' } else { 'False'} } }, 
@{ L = 'QuantityOnHand'; E = { $_.QuantityOnHand } }

$invHash 的示例内容:

SkuID    SkuStatusCode    QuantityOnHand 
-----    -------------    --------------  
1828     True             441 
3022     True             325 
2981     True             214 
2989     True             842 

价格哈希:

    $priceHash = $priceXML.PricingDto.ProductPricingItem.SkuPricingItem | 
select @{ L = 'SkuID'; E = { $_.SkuId } }, @{ L = 'RegularPrice'; 
E = { $_.PriceGroup.RegularPrice } }, @{ L = 'CurrentPrice'; 
E = { $_.PriceGroup.CurrentPrice } } 

$priceHash 的示例内容:

 SkuID    RegularPrice     CurrentPrice 
 -----    -------------    --------------  
 1828     49.99            48.99
 3022     25               19.99
 2981     45               39.99
 2989     28               18.99

$invpriceHash 的所需内容:

SkuID    SkuStatusCode    QuantityOnHand  RegularPrice     CurrentPrice   
-----    -------------    --------------  --------------  --------------
1828     True             441             49.99            48.99
3022     True             325             25               19.99
2981     True             214             45               39.99
2989     True             842             28               18.99
4

1 回答 1

5

给定:

f1.csv 是:

SkuID,SkuStatusCode,QuantityOnHand
1828,True,441 
3022,True,325 
2981,True,214 
2989,True,842 

f2.csv 是:

SkuID,RegularPrice,CurrentPrice
1828,49.99,48.99
3022,25,19.99
2981,45,39.99
2989,28,18.99

试试这个牵强的解决方案,不如join-object你需要知道属性那么好。您还必须小心+操作之间的操作符$a$b它不是可交换的,它会更改组顺序:

   $a = Import-Csv C:\temp\f1.csv
   $b  = Import-Csv C:\temp\f2.csv
   $b + $a | Group-Object -Property skuId  | 
% {$x= New-Object -TypeName psCustomObject -Property 
@{SkuID=$_.name;RegularPrice=$_.group[0].RegularPrice;
CurrentPrice=$_.group[0].CurrentPrice;
SkuStatusCode=$_.group[1].SkuStatusCode;QuantityOnHand=$_.group[1].QuantityOnHand};
 $x}

对我来说,它给出了:

QuantityOnHand : 441 
RegularPrice   : 49.99
SkuStatusCode  : True
SkuID          : 1828
CurrentPrice   : 48.99

QuantityOnHand : 325 
RegularPrice   : 25
SkuStatusCode  : True
SkuID          : 3022
CurrentPrice   : 19.99

QuantityOnHand : 214 
RegularPrice   : 45
SkuStatusCode  : True
SkuID          : 2981
CurrentPrice   : 39.99

QuantityOnHand : 842 
RegularPrice   : 28
SkuStatusCode  : True
SkuID          : 2989
CurrentPrice   : 18.99
于 2013-06-10T15:41:49.663 回答