Join syntax for two arrays of objects

Sep 14, 2012 at 6:58 AM

Hello - I am trying to join the following two javascript arrays to produce an array of objects with the properties of both.


var jsArrayA = [{ "projectid": 122, "projecttype": "radio" },{ "projectid": 133, "projecttype": "tv" }];

var jsArrayB = [ { "actionid": 1, "name": "kuow", "pid": 122 }, { "actionid": 2, "name": "kplu", "pid": 122 }, { "actionid": 3, "name": "abc", "pid": 133 }, { "actionid": 4, "name": "espn", "pid": 133 } ];

var queryResult2 = Enumerable.From(jsArrayA)
    .Join(Enumerable.From(jsArrayB), "$.projectid", "$.pid", "outer,inner=>outer,inner")

The join seems to work, but the array produces contains only the object properties jsArrayB. I assume my syntax is wrong, probably in the  result selector, but I have been unable to resolve.

What I want is each object in the resulting array to have the following properties: projectid, projecttype, actionid, name, pid.

Can someone demonstrate how this should work?

Sep 14, 2012 at 3:50 PM

Yes, resultSelector is bad.
"outer,inner=>outer,inner" is function(outer, inner) { return outer, inner; }
Can you explain this result?
This returns inner because "comma operator" returns last right value.

If you want to simulate C#'s anonymous type then create object.

function(outer, inner) { return { outer:outer, inner:inner } }
"outer, inner => { outer:outer, inner:inner }"
"{ outer:$, inner:$$ }"

so the finally

var queryResult2 = Enumerable.From(jsArrayA)
    .Join(jsArrayB, "$.projectid", "$.pid", "{projectid:$.projectid, projecttype:$.projecttype, actionid:$$.actionid, name:$$.name, pid:$$.pid}")


var queryResult2 = Enumerable.From(jsArrayA)
    .Join(jsArrayB, "$.projectid", "$.pid", function (a, b) {
        return {
            projectid: a.projectid,
            projecttype: a.projecttype,
            actionid: b.actionid,

maybe anonymous function better readble than longest string lambda.

Sep 14, 2012 at 11:01 PM

Thanks very much for the example.  It works great.

linq.js is fabulous!

Sep 19, 2012 at 9:16 PM

If it's not too much to ask, could you illustrate the similar syntax for GroupJoin?

Sep 24, 2012 at 8:12 PM

GroupJoin is similar to SQL's left outer join.
But joined result is structured.

// outer, inner is one value. not sequence. result is flatten.
outerSeq.join(innerSeq, outerKeySelector, innerKeySelector, function(outer, inner) {} );

// outer is one value but groupedInner is grouped sequence. result is not flatten.
outerSeq.groupJoin(innerSeq, outerKeySelector, innerKeySelector, function(outer, groupedInner) {} );

Jan 31, 2013 at 10:48 AM
Please please tell me a sample of the GroupJoin. (at same array objects)