• 关于PowerShell调用Linq的一组实验


    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 方法才能将其转化为“二维数组”。

  • 相关阅读:
    ERROR 1406 : Data too long for column 解决办法
    Sublime配置Python编译环境
    python下载包的时候,如何选择是win32,还是amd64的,其中的cp又是什么意思?
    曝光一个网站,我周末就耗在上面了。(学习)
    Matlab 画图时中文显示乱码的问题?(设置为“桌面代码”修改时区后还是乱码使用这个方法)
    什么是前端和后端开发,看完你就知道了
    彻底卸载mysql 个人亲测!
    python语言的优缺点
    阿里云、华为云和腾讯云等多家物联网平台的异同
    nfs Read only system 问题解决 + NFS 安装
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/13070863.html
Copyright © 2020-2023  润新知