在PowerShell中的HashSet:集合大小是固定的

6

我有一个 PowerShell 函数如下:

Function GetAllIdentityProvidersFromDatabase {

    param (
        [string] $SQLConnectionSting
    )

    $AllIdPIdentifiers = New-Object 'System.Collections.Generic.HashSet[string]'
    $SQLConnect = new-object system.data.sqlclient.sqlconnection $SQLConnectionSting

    try {
        $SQLQuery = $("SELECT [IdPIdentifier] FROM [dbo].[IdPs]")


        $SQLConnect.Open()  

        $command = New-object system.data.sqlclient.SqlCommand   
        $command.connection = $SQLConnect  
        $command.CommandText = $SQLQuery
        $Reader = $command.ExecuteReader()
        while ($Reader.Read()) {
            $value = $Reader.GetValue($1)
            $AllIdPIdentifiers.Add($value) | Out-Null
        }

        $AllIdPIdentifiers

    } catch {
        Write-Host "SQL Select error: " $Error[0].ToString() -ForegroundColor Red
    } finally {
        $SQLConnect.Close()
    }
}

然后,在另一个脚本中:

$AllIdPIdentifiers = New-Object 'System.Collections.Generic.HashSet[string]'
$AllIdPIdentifiers = GetAllIdentityProvidersFromDatabase $SQLConnectionString
$AllIdPIdentifiers.Remove("GodspeedYou")
Write-Host $AllIdPIdentifiers.Count

执行时,我遇到了这个错误:
Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."
At C:\PowerShell\EduGain\FederationMetadataExtractor.ps1:151 char:1
+ $AllIdPIdentifiers.Remove("GodspeedYou")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException

有没有一种方法可以允许“删除”操作?

2
GetAllIdentityProvidersFromDatabase 返回的不是 HashSet,而是 string。如果有多个字符串,则会打包成数组。而数组是固定大小的。 - user4003407
@PetSerAl:假设您是正确的,如果您能详细解释一下为什么该函数返回一个 string 对象数组,那将会很有帮助。考虑到该函数似乎计算出 $AllIdPIdentifiers 的值,并引用了一个 HashSet<T> 对象,我天真地认为该函数本身将返回 HashSet<T> 对象。如果不能使用 OP 显示的代码,有没有一种方法使 PowerShell 函数返回实际的 HashSet<T> 对象呢? - Peter Duniho
1
使用我的函数返回值填充HashSet而不是数组不可能吗?这是为了使用一种数据结构,以最小化ContainsRemove操作的工作量。 - vdenotaris
1个回答

7

当您通过管道传递集合时,它会被枚举,并且每个单独的元素都会被传递。如果您想将集合作为单个元素传递,则应将集合打包到另一个集合中。一元运算符,创建带有单个元素的数组。

Function GetAllIdentityProvidersFromDatabase {

    param (
        [string] $SQLConnectionSting
    )

    $AllIdPIdentifiers = New-Object 'System.Collections.Generic.HashSet[string]'
    $SQLConnect = new-object system.data.sqlclient.sqlconnection $SQLConnectionSting

    try {
        $SQLQuery = $("SELECT [IdPIdentifier] FROM [dbo].[IdPs]")


        $SQLConnect.Open()  

        $command = New-object system.data.sqlclient.SqlCommand   
        $command.connection = $SQLConnect  
        $command.CommandText = $SQLQuery
        $Reader = $command.ExecuteReader()
        while ($Reader.Read()) {
            $value = $Reader.GetValue($1)
            $AllIdPIdentifiers.Add($value) | Out-Null
        }

        ,$AllIdPIdentifiers

    } catch {
        Write-Host "SQL Select error: " $Error[0].ToString() -ForegroundColor Red
    } finally {
        $SQLConnect.Close()
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接