Select Operator Part 4

Select operator is one of the projection operator available in LINQ. Select operator allows you to return an output sequence of certain type by transforming the input sequence using the lambda expression. The output sequence could be of same element type or different but the number of elements returned from the Select operator is always same as the number of elements passed in the input sequence.

The prototype of the select operator looks like this

public static IEnumerable<S> Select<T,S>
(this IEnumerable<T> source,Fund<T,S> selector);

The above extension method is applicable to any object implementing IEnumerable<T>. The select operator is passed in a delegate that enumerates the input sequence of T and returns an output sequence that consists of elements of type S. As discussed earlier T and S could be same type.

public static IEnumerable<S> Select<T,S>
(this IEnumerable<T> source,Fund<T,int,S> selector);

The second prototype shown above is not much different from the first prototype except the predicate takes an additional integer argument which identifies the index of the element in the input sequence.

Let's look through few examples of how we can use select operator in linq to sql, linq to xml and linq to objects.

image

 image

In the above example you would notice I am using the select operator in query syntax to retrieve person whose first name starts with Mike and i am selecting the entire object. If all i want is to retrieve is the first name, I can make use of anonymous type to select only the first name property. This is how you would do it.

image

From the above example, we are creating an anonymous type on the fly which consists of one property called FN that we are assigning using the FirstName property on the person object. Another point worth mentioning is when you are returning anonymous types in the projection, you have to use the var keyword to assign the result to the variable. The reason for this is because, you do not know what is the actual type of the anonymous class is. When you use var keyword, the compiler infers the infers the type from the results.

You can write the same query using method syntax as follows.

image

 

In the above code, we are using method syntax to achieve the same results. Also notice that the output sequence is not of the same type as the input sequence. Input sequence is a collection of Person and the output sequence is of type string.

image

Notice in the above code we are returning the length of the lastName property as integer. This demonstrates that the output sequence does not necessary has to be the same type as the input sequence.

Let's look at example of select operator with predicate containing index of the element.

image

CWindowssystem32cmd.exe

In the above code, we are making use of the indexer and assigning it to the Position property of the anonymous type.  The result as you can see from the output is displaying the index and the Name.

So far we have only looked at examples that made use of select operator in linq to objects, select operator can also be used to select data from xml file or xml string.

 image

This time we are making use of the select operator with xml data. We convert xml data into our predefined objects. by casting the element to right type and assigning it to the appropriate property on our objects.

Not only can you use select operator to convert xml data to objects but you can convert objects to xml by making use of select operator. Let's take a look at an example of that.

image

 

CWindowssystem32cmd.exe (2)

 

In the code above I make use of the select to create xml elements from a collection of person objects in memory. We then output the result of xml to the console.

Not only can you use select operator with xml but you can also use select operator on .net classes.

image

In the above got we are making use of the select operator to only get name of the directories found on C drive.

You can also use select operator to create nested collections and transform the original  hierarchy into a completely new one that meets your needs. Let's look at an example of that.

 

 

image

In the above example we have nested collections making use of nested select statements.In the first select statement we grab the name of the contact and its total order count. We then create a nested collection on the fly from a nested query that creates a collection of orders consisting of orderid and orderdate.

1 Comment

Comments have been disabled for this content.