This project is read-only.

selectMany()

The selectMany linq operator projects each element of the collection into a new sequence and then flattens the resulting sequences into a single sequence. The selectMany operator uses a "collection selector" function to project each element into a sequence. The selectMany operator throws an exception if that collection selector is not given or is not a function.
var arr = [{letter: 'a', numbers: [1, 2, 3]},
    {letter: 'b', numbers: [4, 5, 6]},
    {letter: 'c', numbers: [7, 8, 9]}];
    
// col = [1, 2, 3, 4, 5, 6, 7, 8, 9]
var col = $linq(arr).selectMany("x => x.numbers").toArray();
The selectMany operator also can take a "result selector" function that translates each of the flattened results into some other object. This results selector is passed both the result element and the original object in the linq collection from which the result element originated.
var arr = [{letter: 'a', numbers: [1, 2, 3]},
    {letter: 'b', numbers: [4, 5, 6]},
    {letter: 'c', numbers: [7, 8, 9]}];
    
// col = ['a1', 'a2', 'a3', 'b4', 'b5', 'b6', 'c7', 'c8', 'c9']
var col = $linq(arr).selectMany(
        "x => x.numbers",                   // collection selector
        "(num, obj) => obj.letter + num"    // result selector
    )
    .toArray();

Last edited Sep 7, 2012 at 8:18 PM by battousai999, version 1