This project is read-only.

groupJoin()

The groupJoin linq operator returns a new linq object that contains a "left outer" join of the elements of the originating collection (considered the "outer" collection) and the "inner" collection. The "inner" collection can be anything that the linq.from() method will accept (e.g., an array, a linq object, etc.). The groupJoin operator requires "key selector" functions for both the "outer" and "inner" collections.

The groupJoin operator also requires a "result selector" function that converts the joined results into the final results of the join operation. The result selector is a function that takes as parameters (1) the outer object (of the join) and (2) an array of the joined inner objects (this array will be empty if there are no inner elements associated with the outer element).

The groupJoin operator can also take a "key comparer" function that is used to compare the projected keys for equality.
var companies = [{companyNum: 1, name: 'Google'},
    {companyNum: 2, name: 'Xerox'},
    {companyNum: 3, name: 'Hewlett-Packard'}];
    
var employees = [{company: 1, name: 'John Clark'},
    {company: 2, name: 'Andrew White'},
    {company: 2, name: 'Martin Baker'}];
    
// col1 = ['John Clark at Google', 'Andrew White at Xerox', 'Martin Baker at Xerox', 'nobody at Hewlett-Packard']
var col1 = $linq(companies).groupJoin(employees,
    "x => x.companyNum",    // "outer" key selector (companies)
    "x => x.company",       // "inner" key selector (employees)
    function (outer, inners)
    {
        return $linq(inners)
            .defaultIfEmpty({name: 'nobody'})
            .select(function (x) { return x.name + ' at ' + outer.name; })
            .toArray();
    })
    .selectMany("x => x")
    .toArray();
    
var users = [{username: 'ajones', name: 'Andy Jones'},
    {username: 'kmitchell', name: 'Kevin Mitchell'},
    {username: 'plewis', name: 'Paul Lewis'}];
    
var roles = [{user: 'AJONES', name: 'Admin'},
    {user: 'KMITCHELL', name: 'Developers'},
    {user: 'KMITCHELL', name: 'Testers'}];
    
// col2 = ['Andy Jones (Admin)', 'Kevin Mitchell (Developers)', 'Kevin Mitchell (Testers)', 'Paul Lewis (none)']
var col2 = $linq(users).groupJoin(roles,
    "x => x.username",  // "outer" key selector (users)
    "x => x.user",      // "inner" key selector (roles)
    function (outer, inners)
    {
        return $linq(inners)
            .defaultIfEmpty({name: 'none'})
            .select(function (x) { return outer.name + ' (' + x.name + ')'; })
            .toArray();
    },
    "(x, y) => x.toLowerCase() == y.toLowerCase()")
    .selectMany("x => x")
    .toArray();

Last edited Dec 18, 2013 at 7:24 PM by battousai999, version 5