Take (keyword)
From The Oxygene Language Wiki
This is a Language topic
Feel free to add your notes to this topic below.
The take and skip query operators allow you to work on a subset of a sequence by either taking or skipping a given amount of elements, respectively. The following query will return a sequence with the first 10 customers in the list (or less, if the original list is shorter):
var list := from c in MyCustomers take 10;
In contrast, the next query will skip the first ten customers, returning a query that contains customer 11 and following. If the original list contains only 10 customers or less, the resulting sequence will be empty, of course:
var list := from c in MyCustomers skip 10;
take and skip can be used at various places within a larger query, and their position in the query will affect the result set. For example "take 10 order by c.Name" will return the first 10 customers and order the resulting list, while "order by c.Name take 10" will order the list first, and then take the first 10 ? most likely resulting in customers whose names all start with "A".
As always, the number argument provided to take and skip does not have to be constant, but arbitrary code may be used to obtain the number you need. For example, if MyCustomers was an array, you could write "take MyCustomers.Length/2 " to obtain the first half of the array, and then "skip MyCustomers.Length/2" to get the second half.
Take/Skip While
With the May 2009 Release (3.0.19) we've added support for take while and skip while support. These operators take an expression that evaluates to a boolean. When used, the LINQ classes will skip all records while the skip while condition is true, and after that take all records until take while is false. The following query will return a sequence of customers with ID in range from 10003 to 10005 (inclusive):
var list := from c in MyCustomers order by c.CustomerID skip while c.CustomerID < 10003 take while c.CustomerID < 10006 select c;
See Also
Area: Oxygene Language
Compiler version: Oxygene 5
Language Glossary — Keywords — Types — FAQ — How To