Concat Operator Part 9

Concat is one of the set operators available in Linq. When you work with Linq query operators for sometime, you would notice that there are certain query operators that can all perform a certain job but some operators in certain scenario are easier to work with or more efficient in performing the computation that others. Concat operator allows you to join two sequences together into one single output sequence. You can perform the same operation using selectmany or union operator. The selectmany operator has an advantage that it would allow you to join many sequences into one single sequence.Concat supports joining only two collections and therefore you would have to repeat this operation several times to achieve the same result that selectmany operator would give. Union offers the same functionality with little more power. With union, you can get rid of duplicates that is elements that are common in both sequences. Union uses the default EqualityComparer. If this comparer does not suffice you needs, you have an overloaded option to specify you own IEqualityComparer that defines how to compare two elements to identify if there are same or not.

The prototype of the Concat operator looks like this

public static IEnumerable<T> Concat<T>(
this IEnumerable<T> first,
IEnumerable<T> second);

 

Looking at the prototype above, you would notice contact is an extension method available on any object that implements IEnumerable<T>. It takes another collection that is of the same type and combines both the collection to return one collection of the same type. The output sequence returned from the Concat query operator has the elements in the original order as specified in the original sequence. Thus Concat operator preserves the order of the elements.

 

Let's look through an example of joining two sequences together using all three operators. We will first start off with using Concat and than compare our way through of how we would perform the same operation using other query operators.

image

 

CWindowssystem32cmd.exe

In the above example, we are simply concatenating two sequences together to get one output sequence and prints the results to the output screen.

Let's modify this example to see how you would use Concat to join more than 2 input sequence and what the results would be like if there are duplicates in the collection. I will also make use of other operators using the same example to illustrate how you would use other operators to perform the same task.

image

image

 

In the above example I am making use of all 3 query operators Concat,selectmany and union. In all 3 examples I make use of skip and take operator to create 3 collections on the fly. The first collection makes use of Take operator to take the first 3 elements. I create the second collection by skipping the first 3 and taking the next 3. In order to obtain the third collection I skip the first 6 and get the rest of the elements in the input sequence.

So in the first example with Concat operator, I have to make use of Concat operator twice, first to join the first two collection and than Concat the result with the third collection. The reason I had to use Concat twice is because Concat is limited in its functionality because it allows you to join only two collections at once. 

In the next example I try to perform the same operation but this time I make use of select many operator. Selectmany operator is ideal for flattening out out the hierarchy to generate a single output sequence. I am passing an array that consist of 3 collections to the selectmany operator which when enumerated goes through the delegate that returns a single flattened list .

In third example I make use of union operator which only supports joining two list together. Therefore I have to make use of union twice to join 3 collections into a single sequence. However the output sequence returned is different from the other two examples. Union operator not only joins two list together but it also gets rid of duplicate items from the list. In our example I have 1 number repeated twice in two collections which union gets rid of and adds it to the output sequence only once.

So far we have only see examples of Concat operator with Linq to objects, let's explore an example of making use of Concat operator with Linq to SQL and see how Concat operator gets translated into SQL syntax.

image

CWindowssystem32cmd.exe (3)

In the above example I am making use of Concat operator with Linq to SQL. In order to obtain two distinct collections. I take the first two customers and than merge it with the next two customers following the first two. What is interesting to note is the SQL that IQueryable Provider for Linq to SQL generated to transform Concat into equivalent syntax which SQL server can understand. From the output we can see that Concat operator gets converted to union all operator. If you have used Union all operator in SQL server, you would be aware that union all joins two collection regardless if they have duplicates. If we are change the query to make use of union operator operator the output would make use of union instead of union all operator as shown below.

 

CWindowssystem32cmd.exe (4)

 

Just like many operators have no direct translation in query syntax, Concat operator also does not have any direct translation in query syntax. If you want to make use of Concat operator, you have to resort to method or dot syntax.

2 Comments

  • nice, really nice!


  • I use the following code:

    var obj = from ConductType in FindAllConductType()
    select ConductType;

    var AllObj = obj.Concat(new tb_ConductType[] { new tb_ConductType { ID_uConductType = Guid.Empty, sConductType = "", iPosition = 0, bIsShow = true, dDateCreated = DateTime.Now, FK_uUserCreated = Guid.Empty, sNote = "" } });
    return allObj;

    But it errors: Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.
    Can you help me resolve this error?

Comments have been disabled for this content.