在PowerShell中,我想得到所有使用球的运动的数量。所面临的挑战是,它们的级别低于直接使用Where对象查询所能访问的级别。这是哈希表:
$league = @{
school = @{
name = 'Lincoln High School'
levels = ('9','10','11','12')
}
sports = @{
football = @{
coed = 'b'
season = 'fall'
balls = $true
}
hockey = @{
coed = 'b'
season = 'winter'
balls = $false
}
lacrosse = @{
coed = 'g'
season = 'spring'
balls = $true
}
swim = @{
coed = 'c'
season = 'winter'
balls = $false
}
}
}如何设计查询以跳过balls?每项运动在关键级别上都有不同的名称,通配符似乎不起作用。
( $league.sports | Where-Object { $_.[?].balls -eq $true } ).count我需要一些东西来连接到这项运动的儿童元素。
发布于 2022-03-03 21:28:25
使用.where(..)
($league['sports'].GetEnumerator().Where{$_.Value.Balls}).Count使用Where-Object
($league['sports'].GetEnumerator() | Where-Object {$_.Value.Balls}).Count如您所见,这两种方法都需要使用.GetEnumerator() method。
另一种更冗长的选择是:
$sports = $league['sports']
$sports.PSBase.Keys.Where{$sports[$_]['balls']}.Counthttps://stackoverflow.com/questions/71343929
复制相似问题