代码之家  ›  专栏  ›  技术社区  ›  Tom Tresansky

JSINQ(linqforjavascript库)子查询(how-to)

  •  4
  • Tom Tresansky  · 技术社区  · 14 年前

    我正在使用这个库: jsinq .

    我想使用子查询创建一个新对象。例如,在.NET LINQ中,我可以这样做:

    from a in Attendances
    where a.SomeProperty = SomeValue
    select new {
        .Property1 = a.Property1,
        .Property2 = a.Property2,
        .Property3 = (from p in People
                      where p.SomeProperty = a.Property3
                      select p)
    }
    

    这样我就得到了一个所有人的列表,其中Property3值与列表中返回的每个对象的attention的Property3值相匹配。

    playground

    1 回复  |  直到 14 年前
        1
  •  2
  •   marc_s    9 年前

    在寻找linqjavascript库时,我也从jslinq开始。但是我决定改用linq.js公司我发现它更接近.NET风格的LINQ。

    http://linqjs.codeplex.com

    http://neue.cc/reference.htm

    举个例子,如下linq.js公司以他们发布的查询为例。

    Enumerable.Range(0, 20)
    .Where("$ % 3 == 0")
    .Select("value, index => {index:index, value:value * 10}")
    .WriteLine("$.index + ':' + $.value")
    

    使用输出进行计算:

    0:0
    1:30
    2:60
    3:90
    4:120
    5:150
    6:180
    

    Enumerable.Range(0, 20)
    .Where("$ % 3 == 0")
    .Select("value, index => {index:index, value:Enumerable.Range(0, 20).Where(\"$ % 3 == 0\").ToArray()}")
    .WriteLine("$.index + ':' + $.value") 
    

    退货:

    0:0,3,6,9,12,15,18
    1:0,3,6,9,12,15,18
    2:0,3,6,9,12,15,18
    3:0,3,6,9,12,15,18
    4:0,3,6,9,12,15,18
    5:0,3,6,9,12,15,18
    6:0,3,6,9,12,15,18
    

    这是一个微不足道的例子,但它确实表明子查询可以使用linq.js公司.