Zeeshan Hirani

Senior .net Developer

April 2008 - Posts

ThenByDescending Operator Part 16

ThenByDescending is one of the query operators available in linq. You can use ThenByDescending operator with linq to objects,linq to xml and linq to SQL.ThenByDescending operator is used when you want to sort by more than 1 column or property. You initially start with either orderby or orderbydescending. Since both these operators return IOrderedEnumerable<T>, you cannot reuse these operators again. This where ThenByDescending operator comes into play. ThenByDescending is an extension method which takes a keyselector delegate that returns the key which is used to sort the collection in descending order. The output collection returned from the ThenByDescending operator is of type IOrderedEnumerable<T>. Therefore if you want to sort again in descending order, you can reuse the same operator.

The prototypes for ThenByDescending operator are as follows

public static IOrderedEnumerable<T> ThenByDescending<T, K>(
this IOrderedEnumerable<T> source,
Func<T, K> keySelector)

public static IOrderedEnumerable<T> ThenByDescending<T, K>(
this IOrderedEnumerable<T> source,
Func<T, K> keySelector,
IComparer<K> comparer);

 

The only difference between these two methods are the second takes in a instance of IComparer. You would use that if you want to compare complex objects. In that case you can pass in a custom IComparer which defines how to compare two complex objects.

Let's look at an example of using ThenByDescending operator with linq to objects.

 

image

CWindowssystem32cmd.exe (3)

In the above example I am sorting sequence of names first by name and then by length in descending order. I show the example in both method or dot syntax as well as query syntax.

Let's now see an example of using ThenByDescending operator with linq to xml.

image

In the above example, I am first getting all the product elements. Once I get the product element, I sort the elements based on the name element. Sorting on name is followed by use of ThenByDescending operator to again sort by unitprice. The collection returned is an anonymous type containing Name and UnitPrice property.

You can also use ThenByDescending operator with linq to SQL queries because IQueryable Provider also defines implementation for ThenByDescending operator. ThenByDescending operator gets converted by linq to SQL provider into descending operator which SQL server can understand. Let's have a look at an example of that.

image

In the above example I am returning all the customers sorting by company name followed by contactname in descending order.

OrderByDescending Operator Part 15

OrderByDescending is one of the ordering query operators available in linq.You can use OrderByDescending operator with linq to objects, linq to XML and linq to SQL. OrderByDescending operator can be used in method or dot syntax as well as query syntax which is more of a SQL like syntax.

OrderByDescending operator is an extension method available on any objects that implements IEnumerable<T> or IQueryable<T>. IEnumerable<T> applies to queries based on linq to objects or linq to XML. When OrderByDescending operator is used with IQueryable, the query gets translated to SQL and Linq provider for SQL server transform the OrderByDescending operator into descending keyword which SQL server can understand.

The prototype for OrderByDescending operator looks like this

public static IOrderedEnumerable<T> OrderByDescending<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector)
where
K : IComparable<K>;

Sorting is done based on the key returned by the key selector delegate. OrderByDescending operator requires that keyselector to return a key that must implement IComparable so it can call the compareto method to determine if two elements with in a sequence are equal to, less than or greater than. Once the key is returned by the key selector delegate the output sequence is sorted based on the key in descending order. The output returned by OrderByDescending operator implements IOrderedEnumerable<T>. This means that you can only use OrderByDescending in the first sort because it requires the input sequence to implement IEnumerable<T>.

Let's start of with an example that uses linq to objects.

image

CWindowssystem32cmd.exe

In the above example I am sorting all the integers in descending order. I am also showing the example in both query syntax as well as method syntax.

Let's now look at an example of using OrderByDescending operator with linq to XML.

image

CWindowssystem32cmd.exe

In the above example, I make use of orderby query operator in linq to XML to order the products based on unit price in descending order.

We can also use OrderByDescending operator with linq to SQL queries. In that case linq to SQL provider will convert the linq to queries into descending operator which SQL server can understand.

image

CWindowssystem32cmd.exe (2)

In the above example I make use of OrderByDescending operator with linq to SQL query. In the query I am sorting all the products in descending order based on their unit price.

ThenBy Operator Part 14

ThenBy is one of the ordering query operators available in linq. You can use ThenBy operator with linq to objects, linq to XML and linq to SQL. You will typically use ThenBy operator to do secondary sort. ThenBy operator is used to sort by more than 1 property. When you want to sort initially you would start with using the orderby operator. Since orderby operator returns IOrderderedEnumerable<T>, you cannot reuse the orderby operator again because it can be only used on sequences that implement IEnumerable<T>.  This is where the ThenBy operator comes into play. ThenBy operator is an extension method that is available on input sequence that implement IOrderededEnumerable<T> and also returns IOrderedEnumerable<T> which allows you to reuse it multiple times if you want to sort by more than 1 column or property. The prototype of ThenBy operator looks like this

public static IOrderedEnumerable<T> ThenBy<T, K>(
this IOrderedEnumerable<T> source,
Func<T, K> keySelector)
where
K : IComparable<K>;

 

ThenBy operator sorts the input sequence based on key returned by the key selector delegate.  The key returned by the keySelector delegate can be of the same type as the input sequence.  ThenBy operator enforces that the key returned by the key selector delegate must implement IComparable so it can call the compareto method on the element to determine if two elements are equal to each other. ThenBy forces immediate execution of the query. What this means is the query is no longer differed because in order to determine the sorting, it is essential for ThenBy operator to execute the entire query to determine the correct order of elements in the output sequence. The order of elements returned in the output sequence may not not be the same order as the original sequence if the two elements happens to have the same key element upon which you are sorting. Only order that is guaranteed is the order of elements based on the key element returned by the key selector delegate. Let's look at a an example of using ThenBy operator with linq to objects.

image

CWindowssystem32cmd.exe (2)

In the above example I am getting all the processes on the system. I then sort the all the processes first by processname and then by virtual memory. In the first part of the example I use the method syntax in which I sort all the process initially using the orderby query operator. The orderby query operator is then followed by ThenBy query operator to do subsequent sort based on virtual memorysize. The same example is also done using the query syntax as well.

Let's look through an example of using ThenBy operator with linq to XML query.

image

CWindowssystem32cmd.exe (3)

In the above example I use descendants operator to get all the names. I order the names elements first by id attribute and then by the name. From the output below you can see that our output sequence is sorted by id and than by name.

ThenBy query operator can also be used with linq to SQL query. IQueryable provider for linq to SQL converts ThenBy query operator into order by operator which SQL server can understand. Let's look at an example of that.

image

CWindowssystem32cmd.exe

In the above example I am sorting all the categories by category which has the maximum product and than I sort the categories by the categoryname property. Notice as usual to sort the second time I make use of ThenBy operator to sort the collection.

OrderBy Operator Part 13

OrderBy is one of the ordering query operators available in linq. In terms of availability, you can use OrderBy query operator with linq to objects, linq to SQL and linq to XML. Unlike many query operators that are only available in method or dot syntax, OrderBy query operator can also be used in query syntax which is sometimes easier to use than method syntax.

The first prototype for OrderBy operator that we will look is as follows.

public static IOrderedEnumerable<T> OrderBy<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector) where K : IComparable<K>;

From the above prototype, we can see that OrderBy is an extension method that is available on any object that implements IEnumerable<T>. OrderBy operator allows the source sequence to be ordered in ascending order based on the key returned by the keySelector delegate. The key selector delegate gets executed on every element of type T when source sequence gets enumerated. The keySelector delegate returns an element of type K. Type K can be same as T or it can be different. The OrderBy extension method also enforces a constraint that the type of element returned from the keySelector delegate must implement IComparable interface. The reason for that is because, this is how OrderBy operator compares if two elements are same by calling its compareTo method. Looking at the prototype above you will also notice that OrderBy operator returns a collection of IOrderedEnumerable<T>. Therefore if you want to sort again by another key value, you cannot subsequently use OrderBy query operator again since OrderBy query operator requires the collection to implement IEnumerable<T> instead of IOrderedEnumerable<T>.  So if you need to sort again you have to use thenby query operator which can be used on collections that implement IOrderedEnumerable<T> and returns a collection that also implements  IOrderedEnumerable<T>.

Let's see an example of sorting a collection of strings in ascending order, both in method and dot syntax.

image

 

CWindowssystem32cmd.exe (2)

In the above example I am simply ordering a collection of string passing in a lambda expression that evaluates to a string key. Using that key, the collection gets sorted in ascending order. Since OrderBy operator is also available in query syntax, I am also showing the same code in query syntax which gives the same output as shown in the output screen shot.

Let's move forward with a little more different example where we are sorting complex objects and where the elements in the input sequence is not of the same type that is returned by the keySelector delegates. In the above example the collection that we were sorting was of type string and the keySelector delegate was also returning the key that was also string. In the next example I will demonstrate how you can use OrderBy to sort complex objects and that the type returned by the keySelector delegate does not have to be the same type as the input sequence.

image

 CWindowssystem32cmd.exe

In the above example I am using a complex type Person which has two properties, ID and Name. I am then ordering person object based on its ID which is an integer type. I am also showing the same code using the query syntax as well.

What if you want to order the collection using multiple columns or properties? As I have mentioned previously that OrderBy query operator requires input sequence to implement IEnumerable<T> where as the returned collection from OrderBy query operator is IOrderedEnumerable<T>.  Therefore you have to use thenby operator to further sort your objects. Let's look at a simple example of that.

image

CWindowssystem32cmd.exe

I have modified the example slightly where it sorts first based on ID property and than sorts the collection based on Name. Notice that in method syntax I am making use of ThenBy operator to sort the collection second time by Name. The same 2 level sorting is easily accomplished in query syntax by simply appending the additional property with comma preceding it.

So far we have looked at examples of using OrderBy operator with linq to objects, now we will see a simple example of using OrderBy operator with linq to XML.

image

In the above example I am making use of OrderBy operator with linq to XML queries. I demonstrate the example using both method and query syntax.  Notice how I am casting the element to the correct type to gets its value that is used as the keySelector delegate for the OrderBy operator.

Since IQueryable<T> also defines implementation for OrderBy operator we can use OrderBy operator in linq to SQL queries. Linq to SQL converts the OrderBy operator into OrderBy operator that SQL server can understand. Let's look at an example of using OrderBy with Northwind database.

image

CWindowssystem32cmd.exe

In the above example, I am making use of OrderBy operator with linq to SQL query that gets converted to OrderBy query operator in SQL server. Notice that I am first ordering by computed value which is defined by another lambda expression that sums up all the freight charges for all the orders of a particular customer. I then sort my collection using the contact name.

OrderBy operator forces an immediate execution of query for linq to object and XML. It is important that you understand that the query is not differed because the entire collection needs be enumerated before ordering is possible. This can have a negative impact when the collection is huge as in the example below.

image

The above code takes forever to execute despite the fact Take operator uses differed execution plan to only retrieve first 5 integers. The problem is the OrderBy operator which requires the entire collection to be created and enumerated, hence causing the entire query to get executed immediately.

Although OrderBy operator uses default comparer, you also have the option of passing in a custom comparer to comapre two elements in a collection. In the case of linq to SQL, you cannot use a custom comparer because in SQL server there is no concept of custom sort and all you can do is sort by computed value or column in a database. Using a custom comparer in linq to SQL query would cause a runtime exception. Let's look at a simple example of using a custom comparer.

image

CWindowssystem32cmd.exe

In the above example, I make use of out of the box comparer that allows me to sort elements without considering the casing of the names with in the collection. I pass that comparer in the second overloaded version of the OrderBy query operator. Also notice that lower case paul came first and than upper case Paul followed. This is the same ordering that was present in the original collection.  Although the OrderBy operator preserved the original ordering, it is not guaranteed that the original ordering would be adhered if two elements have the same key returned by the key selector. Only ordering that is guaranteed is what is returned as a key by the key selector delegate.

Union Operator Part 12

Union is one of the set query operators available in linq. It appends 2 input sequences together ensuring that duplicates are removed and returns a single output sequence. The prototype for union operator looks like this

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

Union operator is an extension method that is available on any object that implements IEnumerable<T>. Union operator performs the same function as concat operator with a difference that it gets rids of the duplicates elements in both the sequence. It enumerates the first sequence making sure it is not returning an element that has not been previously returned. It then iterates through the second sequence making sure that it is outputting any element that has been previously returned. Union operator uses GetHashCode and Equals method to determine if two elements are same. Let's look at some examples of union operator

image

CWindowssystem32cmd.exe

The above example is pretty simple. All I am doing is simply joining two sequences into one output sequence. If we modify the example where we have duplicates we will notice that union operator will ensure that it would not repeat duplicate items in the output sequence. Here is an example of that.

image

The above example has duplicates with in the collection as well duplicates between the 2 collections. When we apply the union operator, we get rid of duplicates and end up with distinct elements from both the sequence as shown in the output results below.

CWindowssystem32cmd.exe (2)

So far we have looked at examples for union with linq to objects, union operator can also be applied to linq to SQL and Linq provider converts the union linq operator into union operator in SQL server. Let's run through an example of using union operator in linq to SQL.

image

CWindowssystem32cmd.exe (3)

 

In the above example we are making use of Union operator in linq to SQL because IQueryable Provider also defines the implementation of union operator, hence the linq query gets converted into SQL syntax that SQL server can understand. In the example above we are create our first collection by taking the first 50 orders and traversing its Customer relationship to access its companyname. We then union it with a second collection that we create by getting the next 50 orders and traversing its customer property to get to its companyname. Since a customer may have many orders we can without any doubt assume companyname in both the collection has been repeated several times but once we apply the union operator we end up with only 39 unique companynames between total order of 100 as shown in the output screen.

Union operator is one of those query operators which does not have any direct translation in query syntax. Therefore if you want to make use of union operator you have to resort to method or dot syntax as we have been using through out this example.

So far we have looked at examples that made use of union operator in linq to objects and linq to SQL. We can also use union operator with linq to XML. Let's look at an example of that.

image

CWindowssystem32cmd.exe (4)

In the above example, we make use of union operator with linq to XML. I create my first collection by calling descendants on the XML document to grab all the name elements from it and than I take the first 3 names elements from it. Next I apply the union operator to join it to my second collection which consists of all names elements except the first 3. However if you look at the output results, you would notice that we still have duplicates in our results. The reason for this is, Xelement is a complex object type and the way query engine determines if two nodes are equal is by making use of GetHashCode and Equals method. Since Xelement is a reference type, the results are not the same. In a situation like the above, we can use the overloaded version union operator that allows us to pass our own EqualityComparer object in which we can define how two nodes are considered equal. Let's have a look at an example.

image

 

image

CWindowssystem32cmd.exe

In the example above I have created a class called NodeComparer which implements IEqualityComparer and defines the appropriate implementation of how two nodes can be compared. Secondly I am using an overloaded version of the Union operator that allows me to pass in my custom comparer which in this case i am passing an instance of NodeComparer. As you can see from the output, our results are unique and consists of names that are not repeated.

Reverse Operator Part 11

Reverse is one of ordering query operator. Reverse operator simply returns the input sequence in the reverse order. It does not effect the elements in the input sequence in any way. The prototype of the reverse operator looks like this

public static IEnumerable<T> Reverse<T>(
this IEnumerable<T> source);

From the prototype shown above, you will notice that it is an extension method that is available on any object that implements IEnumerable<T>.

Let's look at few examples of reverse operators.

image

CWindowssystem32cmd.exe

 

In the above example I am demonstrating the use of Reverse operator with two different type of collections, the first one is an array of names (string) and the second one is an array of numbers(integers). For both collections, I simply call Reverse on the collection to reverse the elements in the output sequence as shown in the output screen.

Reverse operator has no translation available in linq to SQL. Therefore if you try to use Reverse operator, you would get an runtime exception.

You can also use Reverse operator with linq to XML. Let's look at the example to understand the usage.

image

CWindowssystem32cmd.exe (2)

In the above example, I am using Reverse operator with linq to XML. First I make use of descendants operator to retrieve all the name nodes in the entire XML document. Then I reverse those nodes and apply the projection operator to select the name of each person as shown in the output screen. As you can see from the output on the screen and the original XML document, the names are printed in the reverse order.

SequenceEqual operator Part 10

Sequence operator determines if two sequences are equal to each other.Two collections are considered equal if they have the same number of elements with same values and in present in the same order.

The prototype of Sequence looks like this

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

 

SequenceEqual query operator is an extension method that is available on objects that implement IEnumerable<T>. SequenceEqual operator determines if two sequences are equal to each other.It enumerates through each sequence in parallel and compares each element by calling its Equal method to determine if they are equal. If the sequence are equal, the return value is true otherwise false. Let's see few examples of SequenceEqual to see its usage.

image

 

CWindowssystem32cmd.exe

In the above example, I have tried to cover all the possible combinations where two sequence may be equal or not equal to each other.

The first example is fairly simple where I test if list1 and list2 are equal each other. Since list1 and list2 are equal to each other, the output confirms that list1 and list2 are equal to each other.

In the second example where I make use of list3 and list4, I have modified list4 from list3 by changing the order of the elements. Therefore when I compare list3 and list4, the output shows that list3 and list4 are not equal. This confirms the fact SequenceEqual requires elements in both sequences must be in the same order.

In the third example where I make use of list5 and list6, I have modified list6 from list5 by adding another element. When you run the query, the result shows that list5 and lis6 are not equal. The reason is, Sequence equal also requires that both list have the same number of elements which in the cast of list6 had an extra element resulting in false value.

So far we have compared list which have values types, what happens when our collection contains complex types like students. How would you use SequenceEqual in that case and how would SequenceEqual know if two students are equal. This is where the second overloaded version of the SequenceEqual comes handy.  This is how the second prototype for SequenceEqual looks like

public static bool SequenceEqual<T>(
this IEnumerable<T> first,
IEnumerable<T> second,
IEqualityComparer<T> comparer);

As I mentioned earlier, by default sequence equals calls equals method on two elements to determine if they are equal. However in the case of complex type we can pass in a custom IEqualtiyComparer and specify how two students are considered equal. For the demonstration purposes, I have made a rule that if the student's lastname is same, it is considered the same student. Let's see an example of that.

This is how my complex Student object and StudentEqualityComparer looks like.

image

 

image

CWindowssystem32cmd.exe

 

In the above example I am making use of a complex type called Student which has two properties firstname and lastname. In the list1 and list2 I have two students. The common item in both the list is that both student's lastnames are same in both list. By default both list are different from each other, but I make use of StudentComparer which defines the rule that if a student's lastname is same then it should be considered the same student. Therefore when you run the example the output returns true.

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.

SkipWhile Operator Part 8

SkipWhile Operator skips elements based on the delegate condition. It continues to skip the elements as long as the predicate returns true. As soon as it encounters false statement, it returns rest of the elements. The prototype of the skip operator looks like this

public static IEnumerable<T> SkipWhile<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);

 

From the prototype you would notice that skipwhile is an extension method that is applied on all objects that implement IEnumerable<T>. It takes a predicate that gets executed on every element that gets iterated on the input sequence. If predicate returns true, it skips the element until it evaluates to false; after which it returns rest of the collection. SkipWhile operator has a second prototype which looks like this

public static IEnumerable<T> SkipWhile<T>(
this IEnumerable<T> source,
Func<T, int, bool> predicate);

In the above SkipWhile operator, the predicate gets passed the index of the element in the source collection.

image

CWindowssystem32cmd.exe

 

From the above result, you would notice that we are first ordering the results. Then we apply the skipwhile operator to skip all the elements which are less than 5 and than returning rest of the elements. From the output you would notice that our output sequence contains all the elements that are greater than or equal to 5.

The above example we have seen, made use of skip operator with linq to objects. SkipWhile operator has no direct conversion with linq to sql. Therefore you have to apply another operator called AsEnumerable to hint the compiler that you want to execute this part of the query in memory instead of sending the entire query to database which would result in failure since linq to sql cannot convert skipwhile operator to an operator in sql which sql server can understand. Let's look at an example.

image

 

In the above example we have a mixture of both linq to sql and linq to objects. We are first querying the database for customers. Than in order to apply skip while operator which cannot be translated to appropriate sql statement by linq to sql provider, we make use of AsEnumerable operator to tell the compiler that we want to perform this part of the query in the memory. After that we make use of the skipwhile operator to skip all the customers whose order's sum is greater than 1000 dollars.

skipwhile operator can also be used with linq to xml to skip elements as long as a condition is not met.

image

 

In the above example we are making use of skipwhile operator with linq to xml. Using the descendants property, we grab all the Names from the query and we than apply the skipwhile operator passing in the lambda expression which checks to see if the name starts with A. So it skips all the elements that starts with A, resulting in the last element which returns Scott.

Skip Operator Part 7

Skip is one of the filtering operators available in linq. Skip operator skips the first count elements and returns rest of the elements in the collection. The prototype of the skip operator looks something like this

public static IEnumerable<T> Skip<T>(
this IEnumerable<T> source,
int count);

 

Looking at the prototype, you will notice that it is an extension method that is applied on any object that implements IEnumerable<T>. It takes an integer that specifies how many elements to skip before it starts to return rest of the elements in the collection. If the number of elements to skip is greater than count of elements in the collection, you will get an output sequence that will be an empty collection.

image

CWindowssystem32cmd.exe

In the above example we are skipping the first 3 elements and returning the rest of the elements from the input sequence. Let's change the above example to skip all the elements from the collection by by passing an integer which is greater than the size of the input collection.

image 

CWindowssystem32cmd.exe

I have modified the above example to skip the entire collection by passing an integer which is equal to the size of the input collection. This results in an empty collection. I spiced up the example by adding another operator which actually allows you to return a default value when the collection is empty. Hence you can see in the output the result is -1.

So far we have only looked at example of using skip operator with linq to object. Let's see an example of using skip operator with linq to SQL and see how it gets translated into SQL statement.

image

CWindowssystem32cmd.exe (2)

From the above example, you would notice that skip operator gets translated into row_number function in linq to sql and than a where clause gets applied to show only rows that are greater than the number returned by the row_number function.

image

CWindowssystem32cmd.exe

 

In the above example we are making use of the skip operator with linq to xml. I am first getting all the persons using the descendants operator and then using the skip operator i skip the first element. I then convert the xml element into anonymous type containing FirstName and LastName. From the result you can see I am displaying the firstname of the of the last person in the collection.

More Posts « Previous page - Next page »