Take Operator Part 5
Take is one of the filtering operator available in linq. It returns first n number of elements and discards rest of the collection. The prototype of Take operator looks like this
public static IEnumerable<T> Take<T>(
this IEnumerable<T> source,
int count);
As you can see Take operator is available on all objects implementing IEnumerable of T. Take operator returns first count number of elements from the input sequence. If count is greater than the size on the source collection, than entire source collection is returned. Let's look at few examples of using Take operator.
In the above example we take the first 3 numbers from a collection of integers. In the example right after that, we take first 10 numbers from the collection. Although the number of elements to take is greater than the size of the collection, the query does not fail and returns the entire collection back.
Take operator is only available in method syntax and has no translation in query syntax. You can use take operator with several other operators to get the result that you want. Let's modify the above example to use take with select operator to return the first person's first name.
In the above example I am first ordering the collection of persons by first name. As a result Mike appears first followed by Scott. After applying the orderby operator, I then take the first object which happens to be mike and pass it to the select operator to return the first name property from the person object.
So far we only looked at examples that demonstrated the use of Take operator in linq to objects. Take operator used in linq to sql will allow you do custom paging where you do not have to bring down all the data and page on the client side. Basically Take operator when applied to IQueryable gets converted row_number() for sql server 2005 and top on sql server 2000 to return the first count of records from the database.
Let's see an example of Take operator operator being used to return 20 customers after skipping the first 10.
In the above code, we make use of skip and take operator to return the first 20 customers after skipping the first 10 customers. Since customers implement IQueryable, the query gets converted to linq to sql. In the output screen we are capturing the sql that gets executed on sql server based on our query. Notice that SQL statement makes uses of row_number function available in 2005 for efficient paging. If we were using sql server 2000 database the sql statement would make use of top operator.
Because Take operator when applied to linq to objects makes use of yield operator, the entire execution is differed and therefore entire collection does not have to be enumerated to return the first count of elements from the input sequence. We can demonstrate this behavior with a simple example.
When you run the above code, you will get the results immediately and that is because Take operator is differed and does not require generating the entire collection to return the first 10 numbers.