|
Chapter 3
The Essence of LINQ
Reproduced from the book
Essential LINQ. Copyrightã 2008, Pearson Education,
Inc., 800 East 96th Street, Indianapolis, IN 46240.
<<Previous
Next>>
|
Composable
The last two foundations of LINQ shed light on its flexibility and power. If you
understand these two features and how to use them, you will be able to tap into
some very powerful technology. Of course, this chapter only introduces these
features; they are discussed in more detail in the rest of the book.
LINQ queries are composable: You can combine them in multiple ways, and one
query can be used as the building block for yet another query. To see how this
works, let’s look at a simple query:
The variable that is returned from the query is sometimes called a computation.
If you write a foreach loop and display the address field from
the customers returned by this computation, you see the following output:
265, boulevered Charonne
25, Lauristan
You can now write a second query against the results of this query:
Notice that the last word in the first line of this query is the computation
returned from the previous query. This second query produces the following
output:
25, Lauristan
LINQ to Objects queries are composable because they operate on and usually
return variables of type IEnumerable<T>. In other words, LINQ
queries typically follow this pattern:
IEnumerable<T> query = from x in IEnumerable<T>
select x;
This is a simple mechanism to understand, but it yields powerful results. It
allows you to take complex problems, break them into manageable pieces, and
solve them with code that is easy to understand and easy to maintain. You will
hear much more about IEnumerable<T> in the next chapter.
The next chapter also details a feature called deferred execution. Although it
can be confusing to newcomers, one of the benefits of deferred execution is
that it allows you to compose multiple queries and string them together without
necessarily needing to have each query entail an expensive hit against the
server. Instead, three or four queries can “execute” without ever sending a
query across the wire to your database. Then, when you need to access the
result from your query, a SQL statement is written that combines the results of
all your queries and sends it across the wire only once. Deferred execution is
a powerful feature, but you need to wait until the next chapter for a full
explanation of how and why it works. The key point to grasp now is that it
enables you to compose multiple queries as shown here, without having to take
an expensive hit each time one “executes.”
<<Previous
Next>>
Also read
Explain the concepts and capabilities of Aspect-Oriented Programming, AOP.
What is Aspect in AOP?
AOP approach addresses Crosscutting concerns. Explain
The components of AOP are advices/interceptors, introductions, metadata, and
pointcuts. Explain them
AOP vs OOPs...........
|