Really basic usage - multiple 'table' multiple 'where'

Jun 25, 2013 at 10:48 AM
Edited Jun 25, 2013 at 10:49 AM
Hi,
Coming from normal SQL, I'm having trouble with the basic syntax of JSLinq

Can someone give a jslinq solution to eg "select c, given a":

var a = [{"id":1,"title":"london"},{"id":2,"title":"Glasgow"},{"id":3,"title":"Leeds"}];
var a_b = [{"aid":1,"bid":3},{"aid":2,"bid":1},{"aid":3,"bid":1}];
var b = [{"id":1,"title":"NewYork"},{"id":2,"title":"Shanghai"},{"id":3,"title":"Guangpo"}];
var b_c = [{"bid":1,"cid":3},{"bid":2,"cid":1},{"bid":3,"cid":1}];
var c = [{"id":1,"title":"Moscow"},{"id":2,"title":"Athens"},{"id":3,"title":"Dubai"}];

in sql it would be:

select c.id, c.title
from a, a_b, b, b_c, c
where a.id = a_b.aid
and a_b.bid = b.id
and b.id = b_c.bid
and b_c.cid = c.id
and a.title = "London"

Thanks...
Jun 25, 2013 at 11:16 AM
Edited Jun 25, 2013 at 11:16 AM
My second attempt is failing...
var sample3 = Enumerable.From(a,
a_b,
b,
b_c,
c)
.Where(a.id = a_b.aid)
.Where(a_b.bid = b.id)
.Where(b.id = b_c.bid)
.Where(b_c.cid = c.id)
.Where(a.title = "London")
.Select(a.id, a.title)
.ToArray();
Coordinator
Jun 25, 2013 at 11:26 AM
Is it inner join?
linq.js write manually join.
var a = [{ "id": 1, "title": "london" }, { "id": 2, "title": "Glasgow" }, { "id": 3, "title": "Leeds" }];
var a_b = [{ "aid": 1, "bid": 3 }, { "aid": 2, "bid": 1 }, { "aid": 3, "bid": 1 }];
var b = [{ "id": 1, "title": "NewYork" }, { "id": 2, "title": "Shanghai" }, { "id": 3, "title": "Guangpo" }];
var b_c = [{ "bid": 1, "cid": 3 }, { "bid": 2, "cid": 1 }, { "bid": 3, "cid": 1 }];
var c = [{ "id": 1, "title": "Moscow" }, { "id": 2, "title": "Athens" }, { "id": 3, "title": "Dubai" }];

var query = Enumerable.From(a)
    .Where(function (x) { return x.title == "london"; })
    .Join(a_b, "$.id", "$.aid", function (a, a_b) { return a_b })
    .Join(b, "$.bid", "$.id", function (a_a_b, b) { return b })
    .Join(b_c, "$.id", "$.bid", function (b, b_c) { return b_c })
    .Join(c, "$.cid", "$.id", function (b_c, c) { return { id: c.id, title: c.title } });
        
// [id:1, title:Moscow]
query.ForEach(function (x) { alert(x.id + ':' + x.title) });
Jun 25, 2013 at 12:15 PM
Edited Jun 25, 2013 at 12:42 PM
Thank you very much! Thats all I need to get up and running with linqjs...

One more question...its not essential, but it is closer to natural english (IMO);
Is there a linqjs syntax closer to the posted sql, eg:
Enumerable.From(a, a_b, b, b_c, c)
.Where(a.id == a_b.aid)
.Where(a_b.bid == b.id)
.Where(b.id == b_c.bid)
.Where(b_c.cid == c.id)
.Where(a.title == 'London')
.Select(a.title);
or:
Enumerable.From(a, a_b, b, b_c, c)
.Where($.id == $$.aid)
.Where($$.bid == $$$.id)
.Where($$$.id == $$$$.bid)
.Where($$$$.cid == $$$$$.id)
.Where($.title == 'London')
.Select($.title);
If not, jslinq is still awesome...and thanks again.
Coordinator
Jun 25, 2013 at 1:14 PM
LINQ is not SQL.
Inner join is not the matter that should take first priority.
I will do such an expectation if I see multi arguments From.

From([a], [b, c], [d, e, f]) => [a, b, c, d, e, f]

It's like Concat. (and concat of linq.js v3 accepts multi arguments. Enumerable.from(a).concat(b, c, d, e, f))

and inner join is not natural for JavaScript(and C#)
It comes from limitation of the SQL.