Skip to content
Eugene Sadovoi edited this page Jul 15, 2016 · 3 revisions

Concatenates two sequences.

Syntax

Concat(second)

Parameters

second

The sequence to concatenate to the first sequence.

Return Value

An Enumerable that contains the concatenated elements of the two input sequences.

Remarks

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. The Concat method differs from the Union method because the Concat method returns all the original elements in the input sequences. The Union method returns only unique elements.

Example

The following code example demonstrates how to use Concat to concatenate two sequences.

class Pet {
    constructor(name, age) {
        this.Name = name;
        this.Age = age;
    }
}

var arrayOfCats = [ new Pet("Barley", Age=8 ),
                    new Pet("Boots", Age=4 ),
                    new Pet("Whiskers", Age=1 ) ];

var arrayOfDogs = [ new Pet("Bounder", Age=3 ),
                    new Pet("Snoopy", Age=14 ),
                    new Pet("Fido", Age=9 ) ];

var cats = Enumerable.asEnumerable(arrayOfCats);
var dogs = Enumerable.asEnumerable(arrayOfDogs);

var query = cats.Select(cat => cat.Name).Concat(dogs.Select(dog => dog.Name));

for (let pet of query) {
    console.log(pet);
} 

// This code produces the following output:
//
// Barley
// Boots
// Whiskers
// Bounder
// Snoopy
// Fido

Try it out

Clone this wiki locally