开发者

JSINQ (Linq for JavaScript library) sub-queries (how-to)

I'm using this library: jsinq.

I want to create a new object using subqueries. For example, in .NET LINQ, I could do something like this:

from a in Attendances
where a.SomeProperty = SomeValue
select new {
    .Property1 = a.Proper开发者_Go百科ty1,
    .Property2 = a.Property2,
    .Property3 = (from p in People
                  where p.SomeProperty = a.Property3
                  select p)
}

such that I get a list of ALL people where Property3 value matches the attendance's Property3 value in EACH object returned in the list.

I didn't see any sample of this in the docs or on the playground. Made a couple tries of it and didn't have any luck.

Anybody know if this is possible and how to?


I also started with jslinq when looking for a LINQ-JavaScript library. However I decided to switch to linq.js which I found to be closer to .NET style LINQ.

http://linqjs.codeplex.com

http://neue.cc/reference.htm

One of the best parts of the linq.js library is that it contains a C# like lambda syntax, and you can put subqueries in these lambdas.

For example, take the following linq.js query they've posted as an example.

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

Evaluates with output:

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

Now am example with a subquery:

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

Returns:

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

It is a trivial example, but it does show that subqueries are possible using linq.js.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜