我有一个PowerShell功能如下:Function GetAllIdentityProvidersFromDatabase {param ([string] $SQLConnectionSting)$AllIdPIdentifiers = New-Object System.Collections.Generic.HashSet[string]$SQLConnect = ...
我有一个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
有没有办法允许删除操作?
解决方法:
当您通过管道传递集合时,它将被枚举并传递每个单独的元素.如果要将集合作为单个元素传递,则应将集合打包到另一个集合.一元,用单个元素创建数组.
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()
}
}
沃梦达教程
本文标题为:c# – Powershell中的HashSet:Collection是固定大小的
猜你喜欢
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- user32.dll 函数说明小结 2022-12-26
- Unity3D实现渐变颜色效果 2023-01-16
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- 如何使用C# 捕获进程输出 2023-03-10
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- c# 模拟线性回归的示例 2023-03-14
- Oracle中for循环的使用方法 2023-07-04
- Unity Shader实现模糊效果 2023-04-27
- .NET CORE DI 依赖注入 2023-09-27