Taco_Buffet
Taco_Buffet

Reputation: 236

Convert PowerShell Hashtable and for Loop to C#

Struggling with getting this powershell logic converted over to c#. My main sticking point is the for loop and getting this value converted over: $mProperties.$sFieldQuery.DBFields[$i]

$mProperties = @{}
$sFields = @()
$dbFields = @()
$dbAliasNames = @()

$metaProperties[$sFieldQuery] = @{
                    SFields = $sFields
                    DBFields = $dbFields
                    DBAliasNames = $dbAliasNames
                    DBFieldValues = @()
                    InternalName = ""
                    }

foreach ($sFieldQuery in $mProperties.Keys)
{
    for ($i=0; $i -lt $mProperties.$sFieldQuery.DBAliasNames.Length; $i++)
    {
        $eQuery += ", " + $mProperties.$sFieldQuery.DBFields[$i] + " AS " + $mProperties.$sFieldQuery.DBAliasNames[$i]
    }
}

Upvotes: 0

Views: 540

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174690

Hashtable dot-notation is functionally the same as using an associative index:

$HashTable = @{ "someKey" = "aValue" }

# This
$HashTable."someKey"
# is functionally equivalent to
$HashTable["someKey"]

So with any Dictionary type in C#, just use the index operator [] instead of a .:

Dictionary<string,Dictionary<string,object[]>> mProperties = new Dictionary<string,Dictionary<string,object[]>>();
// populate mProperties...

string eQuery = "";

foreach(string sFieldQuery in mProperties.Keys)
{
    for(int i = 0; i < mProperties[sFieldQuery]["DBAliasNames"].Length; i++)
    {
        eQuery += ", " + mProperties[sFieldQuery]["DBFields"][i] + " AS " + mProperties[sFieldQuery]["DBAliasNames"][i];
    }
}

Upvotes: 1

Related Questions