简体   繁体   中英

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]
    }
}

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];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM