PowerShell equivalent of LINQ Any()?

前端 未结 11 671
灰色年华
灰色年华 2020-12-01 03:29

I would like to find all directories at the top level from the location of the script that are stored in subversion.

In C# it would be something like this

         


        
11条回答
  •  温柔的废话
    2020-12-01 03:57

    A variation on @JaredPar's answer, to incorporate the test in the Test-Any filter:

    function Test-Any {
        [CmdletBinding()]
        param($EvaluateCondition,
            [Parameter(ValueFromPipeline = $true)] $ObjectToTest)
        begin {
            $any = $false
        }
        process {
            if (-not $any -and (& $EvaluateCondition $ObjectToTest)) {
                $any = $true
            }
        }
        end {
            $any
        }
    }
    

    Now I can write "any" tests like

    > 1..4 | Test-Any { $_ -gt 3 }
    True
    
    > 1..4 | Test-Any { $_ -gt 5 }
    False
    

提交回复
热议问题