zoukankan      html  css  js  c++  java
  • 关于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 方法才能将其转化为“二维数组”。

  • 相关阅读:
    基于jquery 的插件,让IE支持placeholder属性
    MongoDB入门_MongoDB安装与配置
    MongoDB入门_MongoDB特色
    MongoDB入门_相关网站
    MongoDB入门_学习目标
    Shell编程
    redis数据类型及基本命令
    redis配置文件详解
    redis命令
    安装运行redis
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/13070863.html
Copyright © 2011-2022 走看看