2010-03-31 48 views
4

我在使用此庫:jsinqJSINQ(Linq for JavaScript library)子查詢(操作指南)

我想使用子查詢創建一個新的對象。例如,在.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值中的每個對象考勤的Property3值相匹配的列表,在列表中返回。

我沒有在文檔或playground上看到任何樣本。做了幾次嘗試,並沒有任何運氣。

有人知道這是否可能以及如何?

+1

'//表達式解析器可能對未來實現嵌套查詢也很有幫助。「我想,源代碼中的這段文字意味着這次你倒黴了。 – c69 2011-09-16 11:07:01

回答

2

我也從jslinq開始尋找一個LINQ-JavaScript庫。但是,我決定切換到linq.js,我發現它更接近.NET風格的LINQ。

http://linqjs.codeplex.com

http://neue.cc/reference.htm

其中linq.js庫的最好的部分是,它包含了一個C#像lambda語法,你可以把子查詢在這些lambda表達式。

例如,以他們發佈的以下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.