-
-
Notifications
You must be signed in to change notification settings - Fork 13
IEnumerable.groupJoin() method
Marcel Kloubert edited this page Sep 30, 2015
·
11 revisions
IEnumerable->groupJoin($inner, $outerKeySelector, $innerKeySelector, $resultSelector [, $keyComparer]) method
Correlates the elements of that sequence and another based on matching keys and groups them (s. GroupJoin()).
public function groupJoin(sequence $inner,
callable $outerKeySelector,
callable $innerKeySelector,
callable $resultSelector
[, equality_comparer $keyComparer = null ]) : IEnumerable;
Name | Type | Description |
---|---|---|
$inner | [[sequence | Sequence]] |
$outerKeySelector | [[callable | Callable]] |
$innerKeySelector | [[callable | Callable]] |
$resultSelector | [[callable | Callable]] |
$keyComparer | [[equality_comparer | Equality comparer]] |
The other sequence.
The key selector for an item of that sequence that should be compared with the key of an item from the other sequence:
function (mixed $item, IIndexedItemContext $ctx) : mixed;
The current outer item.
The item context for $item
.
The key selector for an item of the other that should be compared with the key of an item from that sequence:
function (mixed $item, IIndexedItemContext $ctx) : mixed;
The current inner item.
The item context for $item
.
The selector that returns the result of corresponing items from the sequences.
function (mixed $outerItem, IEnumerable $innerItems) : mixed;
An item from that sequence.
The corresponding items of $outerItem
from the other sequence.
The equality comparer that checks if two item keys are equal or not.
The new sequence.
use \System\Collections\IEnumerable;
use \System\Linq\Enumerable;
class Person {
public function __construct($name) {
$this->Name = $name;
}
public $Name;
}
class Pet {
public function __construct($name, Person $owner) {
$this->Name = $name;
$this->Owner = $owner;
}
public $Name;
public $Owner;
}
$persons = [new Person("Tanja"),
new Person("Marcel"),
new Person("Yvonne"),
new Person("Josefine")];
$pets = [new Pet("Gina" , $persons[1]),
new Pet("Schnuffi" , $persons[1]),
new Pet("Schnuffel", $persons[2]),
new Pet("WauWau" , $persons[0]),
new Pet("Lulu" , $persons[3]),
new Pet("Sparky" , $persons[0]),
new Pet("Asta" , $persons[1])];
$personSeq = Enumerable::create($persons);
$joined = $personSeq->groupJoin($pets,
'$person => $person->Name',
'$pet => $pet->Owner->Name',
function(Person $person, IEnumerable $pets) {
$petList = $pets->select('$x => $x->Name')
->joinToString('", "');
return sprintf('Owner: %s; Pets: "%s"',
$person->Name,
$petList);
});
foreach ($joined as $item) {
// [0] 'Owner: Tanja; Pets: "WauWau", "Sparky"'
// [1] 'Owner: Marcel; Pets: "Gina", "Schnuffi", "Asta"'
// [2] 'Owner: Yvonne; Pets: "Schnuffel"'
// [3] 'Owner: Josefine; Pets: "Lulu"'
}