iRon7 / Join-Object

Combines two objects lists based on a related property between them.
MIT License
107 stars 13 forks source link

Smarter property merge #12

Closed iRon7 closed 4 years ago

iRon7 commented 4 years ago

The idea started from the stackoverflow question: How to combine items from one PowerShell Array and one Powershell Object and produce a 3rd PowerShell Object?:

$vmSizelist = ConvertFrom-SourceTable '
    Name VMSize          ResourceGroup 
    VM1  Standard_D2s_v3 RG1
    VM2  Standard_D14_v2 RG2'

$AllVMSize = ConvertFrom-SourceTable '
    Name            NumberOfCores MemoryInMB MaxDataDiskCount OSDiskSizeInMB ResourceDiskSizeInMB
    Standard_B1ls               1        512                2        1047552                 4096
    Standard_B1ms               1       2048                2        1047552                 4096
    Standard_B1s                1       1024                2        1047552                 4096
    Standard_B2ms               2       8192                4        1047552                16384
    Standard_B2s                2       4096                4        1047552                 8192
    Standard_D2s_v3             2       8192                4        1047552                16384
    Standard_D14_v2            16     114688               64        1047552               819200'

$vmSizeList | LeftJoin $AllVMSize -On VMSize -Eq Name | Format-Table

Currently result in:

Name                   VMSize          ResourceGroup NumberOfCores MemoryInMB MaxDataDiskCount OSDiskSizeInMB ResourceDiskSizeInMB
----                   ------          ------------- ------------- ---------- ---------------- -------------- --------------------
{VM1, Standard_D2s_v3} Standard_D2s_v3 RG1                       2       8192                4        1047552                16384
{VM2, Standard_D14_v2} Standard_D14_v2 RG2                      16     114688               64        1047552               819200

Where the right part of the Name property (Name[1]) is actually redundant with the VMSize column because they are related by the -On parameter (where VMSize -Eq Name). In other words, in such a case the name property can be simplified to just: {$Left.$_}:

Name VMSize          ResourceGroup NumberOfCores MemoryInMB MaxDataDiskCount OSDiskSizeInMB ResourceDiskSizeInMB
---- ------          ------------- ------------- ---------- ---------------- -------------- --------------------
VM1  Standard_D2s_v3 RG1                       2       8192                4        1047552                16384
VM2  Standard_D14_v2 RG2                      16     114688               64        1047552               819200

The same goes for the embedded example: Note that related properties with the same name (where the -equals parameter is omitted) are already merged in the current implementation (prior 3.2.4)

PS C:\> $Employee

Id Name    Country Department  Age ReportsTo
-- ----    ------- ----------  --- ---------
 1 Aerts   Belgium Sales        40         5
 2 Bauer   Germany Engineering  31         4
 3 Cook    England Sales        69         1
 4 Duval   France  Engineering  21         5
 5 Evans   England Marketing    35
 6 Fischer Germany Engineering  29         4

PS C:\> $Department

Name        Country
----        -------
Engineering Germany
Marketing   England
Sales       France
Purchase    France

PS C:\> $Employee | InnerJoin $Department -On Department -Equals Name -Discern Employee, Department | Format-Table

Id EmployeeName EmployeeCountry Department  Age ReportsTo DepartmentName DepartmentCountry
-- ------------ --------------- ----------  --- --------- -------------- -----------------
 1 Aerts        Belgium         Sales        40         5 Sales          France
 2 Bauer        Germany         Engineering  31         4 Engineering    Germany
 3 Cook         England         Sales        69         1 Sales          France
 4 Duval        France          Engineering  21         5 Engineering    Germany
 5 Evans        England         Marketing    35           Marketing      England
 6 Fischer      Germany         Engineering  29         4 Engineering    Germany

The DepartmentName column is redundant with the Department column, meaning this can be simplified to:

Id Name    EmployeeCountry Department  Age ReportsTo DepartmentCountry
-- ----    --------------- ----------  --- --------- -----------------
 1 Aerts   Belgium         Sales        40         5 France
 2 Bauer   Germany         Engineering  31         4 Germany
 3 Cook    England         Sales        69         1 France
 4 Duval   France          Engineering  21         5 Germany
 5 Evans   England         Marketing    35           England
 6 Fischer Germany         Engineering  29         4 Germany

As the Department column and DepartmentName column are equal, meaning that only the Country column is divided in two (EmployeeCountry and DepartmentCountry) columns

iRon7 commented 4 years ago

Implemented