-
Notifications
You must be signed in to change notification settings - Fork 26
Join
Correlates the elements of two sequences based on matching keys.
Join(inner, sourceKeySelector, innerKeySelector, resultSelector)
The sequence to join to the source sequence.
A function to extract the join key from each element of the source sequence:
TSourceKey sourceKeySelector(TSource)
A function to extract the join key from each element of the inner sequence:
TInnerKey innerKeySelector(TInner)
A function to create a result element from two matching elements:
TResult resultSelector(TSource, TInner)
An Iterable
that has elements of type TResult
that are obtained by performing an inner join on two sequences.
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated.
A join
refers to the operation of correlating the elements of two sources of information based on a common key. Join
brings the two information sources and the keys by which they are matched together in one method call. This differs from the use of SelectMany
, which requires more than one method call to perform the same operation.
Join
preserves the order of the elements of outer, and for each of these elements, the order of the matching elements of inner. In relational database terms, the Join
method implements an inner join. 'Inner' means that only elements that have a match in the other sequence are included in the results. An join
is a join in which the keys are compared for equality. A left outer join operation has no dedicated standard query operator, but can be performed by using the GroupJoin
method.
The following code example demonstrates how to use Join
to perform an inner join of two sequences based on a common key.
class Person {
constructor(name) {
this.Name = name;
}
}
class Pet {
constructor(name, owner) {
this.Name = name;
this.Owner = owner;
}
}
var magnus = new Person("Hedlund, Magnus");
var terry = new Person("Adams, Terry");
var charlotte = new Person("Weiss, Charlotte");
var barley = new Pet("Barley", terry);
var boots = new Pet("Boots", terry);
var whiskers = new Pet("Whiskers", charlotte);
var daisy = new Pet("Daisy", magnus);
var petsArray = [ barley, boots, whiskers, daisy ];
var peopleArray = [ magnus, terry, charlotte ];
var people = Enumerable.asEnumerable(peopleArray);
var pets = Enumerable.asEnumerable(petsArray);
// Create a list of Person-Pet pairs where
// each element is an anonymous type that contains a
// Pet's name and the name of the Person that owns the Pet.
var query =
people.Join(pets,
person => person,
pet => pet.Owner,
(person, pet) => {
return { OwnerName: person.Name, Pet: pet.Name }; });
for (var obj of query) {
console.log(obj.OwnerName + " - " + obj.Pet);
}
/*
This code produces the following output:
Hedlund, Magnus - Daisy
Adams, Terry - Barley
Adams, Terry - Boots
Weiss, Charlotte - Whiskers
*/