惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
月光博客
月光博客
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
宝玉的分享
宝玉的分享
A
About on SuperTechFans
Vercel News
Vercel News
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
Jina AI
Jina AI
Y
Y Combinator Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI

VnYzm的博客

游戏玩后感:ReLief:献给亲爱的你 我的周边(谷子)分享 游戏玩后感:Kanon 简谱:致真实的你 《Rust中常见的有关生命周期的误解》学习笔记 简谱:StarMap 简谱:かく咲きたらばいと恋ひめやも 简谱:东风 简谱:无法诉说的思念 简谱:Girlish 游戏玩后感:时钟机关的Layline 简谱:风之琶音 简谱:星空的记忆 简谱:因为遇见了你 简谱:月童 番茄简谱脚本转调器 游戏玩后感:青空下的约定:Refine 游戏玩后感:在这苍穹展翅 书籍读后感:控制论与科学方法论 游戏玩后感:恋爱表达式 游戏玩后感:樱之诗 MLIR-tutorial学习笔记 游戏玩后感:潜伏之赤途 游戏玩后感:纯爱咖啡厅:帕露菲重制版 游戏玩后感:智以泪聚 游戏玩后感:初雪樱 游戏玩后感:告别回忆:从今以后 游戏玩后感:梦灯花 游戏玩后感:金辉恋曲四重奏 游戏玩后感:五彩斑斓的世界
关于PowerShell调用Linq的一组实验
VnYzm · 2020-05-01 · via VnYzm的博客
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\> $a = 0..2
PS C:\> $b = 3..5
PS C:\> $a.tostring()
System.Object[]
PS C:\> $c = [Linq.Enumerable]::Zip($a, $b, [Func[Object, Object, Object]]{$args[0]+$args[1]})
PS C:\> $c
3
5
7
PS C:\> $c[0]
3
5
7
PS C:\> $c.tostring()
System.Linq.Enumerable+<ZipIterator>d__61`3[System.Object,System.Object,System.Object]
PS C:\> $d = $c | % { $_ }
PS C:\> $d[0]
3
PS C:\> $d.tostring()
System.Object[]
PS C:\> $c = [Linq.Enumerable]::Zip($a, $b, [Func[Object, Object, Object]]{ @{ i=$args[0]; j=$args[1] } })
PS C:\> $c

Name                           Value
----                           -----
j                              3
i                              0
j                              4
i                              1
j                              5
i                              2


PS C:\> $c[0]

Name                           Value
----                           -----
j                              3
i                              0
j                              4
i                              1
j                              5
i                              2


PS C:\> $c.i
0
1
2
PS C:\> $c.tostring()
System.Linq.Enumerable+<ZipIterator>d__61`3[System.Object,System.Object,System.Object]
PS C:\> $d = $c | % { $_ }
PS C:\> $d

Name                           Value
----                           -----
j                              3
i                              0
j                              4
i                              1
j                              5
i                              2


PS C:\> $d[0]

Name                           Value
----                           -----
j                              3
i                              0


PS C:\> $d[0].i
0
PS C:\> $d.tostring()
System.Object[]
PS C:\> $c = [Linq.Enumerable]::Zip($a, $b, [Func[Object, Object, Object]]{ ($args[0], $args[1]) })
PS C:\> $c
0
3
1
4
2
5
PS C:\> $c.tostring()
System.Linq.Enumerable+<ZipIterator>d__61`3[System.Object,System.Object,System.Object]
PS C:\> $c[0]
0
3
1
4
2
5
PS C:\> $d = $c | % { $_ }
PS C:\> $d
0
3
1
4
2
5
PS C:\> $d[0]
0
PS C:\> $d[1]
3
PS C:\> $d.tostring()
System.Object[]
PS C:\> $d = [Linq.Enumerable]::ToArray($c)
PS C:\> $d[0]
0
3
PS C:\> $d[0][0]
0
PS C:\> $d.tostring()
System.Object[]
PS C:\> $c = [Linq.Enumerable]::Zip($a, $b, [Func[Object, Object, int[]]]{($args[0], $args[1])})
PS C:\> $c[0]
0
3
1
4
2
5
PS C:\> $c.tostring()
System.Linq.Enumerable+<ZipIterator>d__61`3[System.Object,System.Object,System.Int32[]]
PS C:\> $d = $c | % { $_ }
PS C:\> $d[0]
0
PS C:\> $d[1]
3
PS C:\> $d.tostring()
System.Object[]
PS C:\> $d = [Linq.Enumerable]::ToArray($c)
PS C:\> $d[0]
0
3
PS C:\> $d[0][0]
0
PS C:\> $d.tostring()
System.Int32[][]
PS C:\> $c = [Linq.Enumerable]::Zip($a, $b, [Func[Object, Object, Object]]{$l = [Collections.Generic.List[int]]::new(); $l.Add($args[0])
; $l.Add($args[1]); $l})                                                                 PS C:\> $c[0]
0
3
1
4
2
5
PS C:\> $d = $c | % { $_ }
PS C:\> $d[0]
0
PS C:\> $d[1]
3
PS C:\> $d.tostring()
System.Object[]
PS C:\> $d = [Linq.Enumerable]::ToArray($c)
PS C:\> $d[0]
0
3
PS C:\> $d[0][1]
3

调用Linq返回的那个类型我不太清楚,但应该是一个实现了IEnumerable的类,不能直接当作数组使用,但可以用For-Object(%)进行迭代。因此可见,当Zip内部匿名函数返回的是非可迭代类型时,可以使用For-Object(%)命令将Zip返回的类转化为由可迭代类型的数组,而当返回的是可迭代类型,使用For-Object(%)命令会将Zip返回的类平摊为一个一维数组,这样就不能达到我们的要求,需要使用[Linq.Enumerable]::ToArray 方法才能将其转化为“二维数组”。