Partitioning operators allow accomplishing a technique known as paging, which is common in data access scenarios. There are two main operators in LINQ: Skip
and Take
, in which Skip
avoids selecting the specified number of elements, and Take
puts the specified number of elements into a sequence. Code in Listing 24.3 shows an example of paging implementation using the two operators.
The private field pageCount
acts as a counter. According to its value, the query skips the number of elements already visited represented by the value of pageCount
. If no elements were visited, the query skips nothing. The code invoking QueryProducts
increase or decrease by 10 units the pageCount
value depending if you want to move forward or backward to the collection items.
In this chapter you got a high-level overview of LINQ key concepts. In this particular discussion you learned about LINQ to Objects as the built-in provider for querying in-memory collections seeing LINQ in action via specific Visual Basic keywords that recall the SQL syntax, such as From
, Select
, Where
, and Join
. You can build LINQ queries while writing Visual Basic code, taking advantage of the background compiler, IntelliSense, and CLR control. Such queries written in the code editor are known as query expressions. Query expressions generally return an IEnumerable(Of T),
but they are not executed immediately. According to the key concept of deferred execution, LINQ queries are executed only when effectively utilized, and this is something that you find in subsequent LINQ providers. With LINQ you can build complex query expressions to query your data sources; this is accomplished via the standard query operators, which are covered in the last part of the chapter. LINQ to Objects is the basis of LINQ, and most of the concepts shown in this chapter will be revisited in next ones.
18.216.88.54