join()

The join linq operator returns a new linq object that contains an "inner 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 join operator requires "key selector" functions for both the "outer" and "inner" collections. The join operator also requires a "result selector" function that converts the joined results into the final results of the join operation. The join 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']
var col1 = $linq(companies).join(employees,
    "x => x.companyNum",    // "outer" key selector (companies)
    "x => x.company",       // "inner" key selector (employees)
    "(outer, inner) => inner.name + ' at ' + outer.name")
    .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)']
var col2 = $linq(users).join(roles,
    "x => x.username",  // "outer" key selector (users)
    "x => x.user",      // "inner" key selector (roles)
    "(outer, inner) => outer.name + ' (' + inner.name + ')'",
    "(x, y) => x.toLowerCase() == y.toLowerCase()")
    .toArray();

Last edited Dec 18, 2013 at 6:25 PM by battousai999, version 5