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

Description:

This method's parameter is used to extract a child collection from each parent item. Finally all of the child collections will be combined into one collection containing all previews collections items, in flattened format. In following sample, when the SelectMany method is used to read the Skills collections , the four set of skills are extracted and the combined into a single array.

Note that you can pas a resultSelector at the end of arguments list, so you can project the final result of selectMany. So you can avoid from calling select method after selectMany in case you want projected results.

Samples:

let employeesIncudeSkills = [
  { name: 'Bob', title: 'Senior Developer', salary: 40000, skills: ['ASP.NET', 'C#', 'JavaScript', 'SQL', 'XML'] },
  { name: 'Sam', title: 'Developer', salary: 32000, skills: ['ASP.NET', 'C#', 'Oracle', 'XML'] },
  { name: 'Mel', title: 'Developer', salary: 29000, skills: ['C#', 'C++', 'SQL'] },
  { name: 'Jim', title: 'Junior Developer', salary: 20000, skills: ['HTML', 'Visual Basic'] }
];

let skills = employeesIncudeSkills.asEnumerable()
  .selectMany(emp => emp.skills)
  .toArray();

console.log("*** using selectMany method to combine skills ...");
for (let index = 0; index < skills.length; index++)
  console.log(skills[index]);
Clone this wiki locally