Skip to content
Yasser Moradi edited this page Aug 30, 2015 · 3 revisions

Description:

These allow two collections to be combined in a join operation based on matching key values. Then the results will be grouped into keyed collections that may be aggregated. A grouped join provides similar functionality to grouping and joining. An outer list and an inner list will be joined into a single entity and then grouped so that each outer element is paired with the list of matching inner items

Note that there is an optional argument, named comparer at the end of arguments list, which accepts a function to make keys comparison customizable.

Samples:

let categories = [
  { name: 'Dairy', majorCategory: 'Chilled' },
  { name: 'Fruit', majorCategory: 'Fresh' },
  { name: 'Vegetable', majorCategory: 'Fresh' }
];

let stock = [
  { name: 'Apple', category: 'Fruit', price: 0.30 },
  { name: 'Banana', category: 'Fruit', price: 0.35 },
  { name: 'Orange', category: 'Fruit', price: 0.29 },
  { name: 'Cabbage', category: 'Vegetable', price: 0.49 },
  { name: 'Carrot', category: 'Vegetable', price: 0.29 },
  { name: 'Lettuce', category: 'Vegetable', price: 0.30 },
  { name: 'Milk', category: 'Dairy', price: 1.12 }
];

let groupJoins = categories.asEnumerable()
  .groupJoin(
    stock.asEnumerable(),
    cat => cat.name,
    stock => stock.category,
    (cat, stocks) => ({
      category: cat.name,
      major: cat.majorCategory,
      stocks: stocks
    })
    ).toArray();

console.log('*** using groupJoin method to show each category and its stocks related ...');

for (let index = 0; index < groupJoins.length; index++) {
  let cat = groupJoins[index];
  console.log('Category name : ' + cat.category + ' , Major : ' + cat.major);
  for (let stockIndex = 0; stockIndex < cat.stocks.length; stockIndex++) {
    let stock = cat.stocks[stockIndex];

    console.log("[" +
      "Name = " + stock.name + "," +
      "Price = " + stock.price +
      "]")
  }
}
Clone this wiki locally