PowerShell equivalent of LINQ Any()?

前端 未结 11 627
灰色年华
灰色年华 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 04:00

    I recommend the following solution:

    <#
    .SYNOPSIS 
       Tests if any object in an array matches the expression
    
    .EXAMPLE
        @( "red", "blue" ) | Where-Any { $_ -eq "blue" } | Write-Host
    #>
    function Where-Any 
    {
        [CmdletBinding()]
        param(
            [Parameter(Mandatory = $True)]
            $Condition,
    
            [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
            $Item
        )
    
        begin {
            [bool]$isMatch = $False
        }
    
        process {
          if (& $Condition $Item) {
              [bool]$isMatch = $true
          }
        }
    
        end {
            Write-Output $isMatch
        }
    }
    
    # optional alias
    New-Alias any Where-Any
    
    0 讨论(0)
  • 2020-12-01 04:04

    It's actually quite simple - just select first $true (formatted for clarity):

    [bool] ($source `
            | foreach { [bool] (<predicate>) } `
            | where { $_ } `
            | select -first 1)
    

    Alternative way:

    ($source `
            | where { <predicate> } `
            | foreach { $true } `
            | select -first 1)
    
    0 讨论(0)
  • 2020-12-01 04:05

    I think that the best answer here is the function proposed by @JaredPar, but if you like one-liners as I do I'd like to propose following Any one-liner:

    # Any item is greater than 5
    $result = $arr | %{ $match = $false }{ $match = $match -or $_ -gt 5 }{ $match }
    

    %{ $match = $false }{ $match = $match -or YOUR_CONDITION }{ $match } checks that at least one item match condition.

    One note - usually the Any operation evaluates the array until it finds the first item matching the condition. But this code evaluates all items.

    Just to mention, you can easily adjust it to become All one-liner:

    # All items are greater than zero
    $result = $arr | %{ $match = $false }{ $match = $match -and $_ -gt 0 }{ $match }
    

    %{ $match = $false }{ $match = $match -and YOUR_CONDITION }{ $match } checks that all items match condition.

    Notice, that to check Any you need -or and to check All you need -and.

    0 讨论(0)
  • 2020-12-01 04:13

    I ended up doing it with a count:

    $directoryContainsSvn = {
        (Get-ChildItem $_.Name -force | ? {$_.PsIsContainer -and $_.Name -eq "_svn" -or $_.Name -eq ".svn"} | Measure-Object).Count -eq 1
    }
    $svnDirs = Get-ChildItem | ? {$_.PsIsContainer} | ? $directoryContainsSvn
    
    0 讨论(0)
  • 2020-12-01 04:15

    You can tighten this up a bit:

    gci -fo | ?{$_.PSIsContainer -and `
                (gci $_ -r -fo | ?{$_.PSIsContainer -and $_ -match '[_.]svn$'})}
    

    Note - passing $__.Name to the nested gci is unnecessary. Passing it $_ is sufficent.

    0 讨论(0)
提交回复
热议问题