简体   繁体   English

Linq.First 等价于 PowerShell 是什么?

[英]What is the Linq.First equivalent in PowerShell?

The snippet below detects from a list of files which of them is a Directory on Ftp下面的代码片段从文件列表中检测到其中哪些是 Ftp 上的目录

as C# it will be like below作为 C# 它将如下所示

var files = new List<string>(){"App_Data", "bin", "Content"};
var line = "drwxr-xr-x 1 ftp ftp              0 Mar 18 22:41 App_Data"
var dir = files.First(x => line.EndsWith(x));

How I can transalte the last line in PowerShell?我如何转换 PowerShell 中的最后一行?

Something like this...像这样的东西...

$files = @("App_Data", "bin", "Content")
$line = "drwxr-xr-x 1 ftp ftp              0 Mar 18 22:41 App_Data"
$dir = $files | Where { $line.EndsWith($_) } | Select -First 1

These versions of the last line would all accomplish the same:最后一行的这些版本都将完成相同的任务:

$dir = @($files | Where { $line.EndsWith($_) })[0]

$dir = $files | Where { $line.EndsWith($_) } | Select -index 0

$dir = $files | Where { $line.EndsWith($_) } | Select -First 1

It was pointed out that the above is not exactly equivalent in behavior to Linq.First because Linq.First throws exceptions in two cases:有人指出,上面的行为并不完全等同于 Linq.First,因为 Linq.First 在两种情况下抛出异常:

  • Throws ArgumentNullException when source or predicate is null.当源或谓词为空时抛出 ArgumentNullException。
  • Throws InvalidOperationException when source sequence is empty or no element satisfies the condition in predicate.当源序列为空或没有元素满足谓词中的条件时,抛出 InvalidOperationException。

If you wanted that behavior exactly, you'd need some extra guard code.如果您完全想要这种行为,则需要一些额外的保护代码。

as Robert Groves said, Select-Object -First Occurence do the tricks, you can also use -Last Occurence.正如罗伯特格罗夫斯所说, Select-Object -First Occurence 做技巧,你也可以使用 -Last Occurence。

by the way, like any other static .Net method you can use linq in powershell.顺便说一下,就像任何其他静态 .Net 方法一样,您可以在 powershell 中使用 linq。

[Linq.Enumerable]::First($list)

[Linq.Enumerable]::Distinct($list)

[Linq.Enumerable]::Where($list, [Func[int,bool]]{ param($item) $item -gt 1 })

Doug Finke produced a great video ( only 7 mins ) about converting C# to Powershell http://dougfinke.com/video/CSharpToPowerShell.html Doug Finke 制作了关于将 C# 转换为 Powershell 的精彩视频(仅 7 分钟) http://dougfinke.com/video/CSharpToPowerShell.html

Roberts example is very good indeed, though comma delimiting will implicitly be treated as an array罗伯茨的例子确实很好,尽管逗号分隔会被隐式地视为一个数组

the shortest way of doing it would be to put it all into a single pipeline :这样做的最短方法是将其全部放入一个管道中:

$dir = "App_Data", "bin", "Content" | % { if("drwxr-xr-x 1 ftp ftp              0 Mar 18 22:41 App_Data".EndsWith($_)) { $_ } } | select -first 1

There is a native way to do this using the Powershell Array's Where Function by passing in a WhereOperatorSelectionMode like this:有一种本机方法可以使用Powershell 数组的 Where Function通过传入WhereOperatorSelectionMode来执行此操作,如下所示:

(1..9).Where({ $_ -gt 3}, "First") # 4

You can also use the mode straight from the enum as well:您也可以直接从枚举中使用模式:

$mode = [System.Management.Automation.WhereOperatorSelectionMode]::First
(1..9).Where({ $_ -gt 3}, $mode) # 4

Using any of the values from the WhereOperatorSelectionMode enum使用WhereOperatorSelectionMode枚举中的任何值

Name名称 Val瓦尔 Description描述
Default 0 Return all items退回所有物品
First 1 Return the first item返回第一项
Last 2 Return the last item返回最后一项
SkipUntil 3 Skip items until condition is true跳过项目直到条件为真
Until 4 Return all items until condition is true返回所有项目直到条件为真
Split 5 Return an array of two elements返回两个元素的数组

See Also : Checking Out The Where and ForEach Operators in PowerShell V4另请参阅检查 PowerShell V4 中的 Where 和 ForEach 运算符

This is a really simple implementation for First :这是First一个非常简单的实现:

function First($collection)
{
    foreach ($item in $collection)
    {
        return $item
    }
    return $null
}

Instead of returning $null , you could throw an InvalidOperationException exception.您可以抛出InvalidOperationException异常,而不是返回$null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM