|
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>>
|
Transformative
SQL is poor at transformations, so we are unaccustomed to thinking about query
languages as a tool for converting data from one format to another. Instead, we
usually use specialized tools such as XSLT or brute-force techniques to
transform data.
LINQ, however, has transformational powers built directly into its syntax. We
can compose a LINQ query against a SQL database that effortlessly performs a
variety of transforms. For instance, with LINQ it is easy to transform the
result of a SQL query into a hierarchical XML document. You can also easily
transform one XML document into another with a different structure. SQL data is
transformed into a hierarchical set of objects automatically when you use LINQ
to SQL. In short, LINQ is very good at transforming data, and this adds a new
dimension to our conception of what we can do with a query language.
Listing 3.2 shows code that takes the results of a query against relational data
and transforms it into XML.
Embedded in this query is a simple LINQ to SQL query that returns the Address
and City fields from all the customers who live in Paris. In Listing 3.3 I’ve
stripped away the LINQ to XML code from Listing 3.2 to show you the underlying
LINQ to SQL query.
LINQ is constantly transforming one type of data into another type. It takes
relational data and transforms it into objects; it takes XML and transforms it
into relational data. Because LINQ is extensible, it is at least theoretically
possible to use it to tear down the walls that separate any two arbitrary data
domains.
Because LINQ is both composable and transformative, you can use it in a number
of unexpected ways:
• You can compose multiple queries, linking them in discrete chunks. This often
allows you to write code that is easier to understand and maintain than
traditional nested SQL queries.
• You can easily transform data from one data source into some other type. For
instance, you can transform SQL data into XML.
• Even if you do not switch data sources, you can still transform the shape of
data. For instance, you can transform one XML format into another format. If
you look back at the section “Declarative: Not How, But What,” you will see
that we transformed data that was stored in nested lists into data that was
stored in a single list. These kinds of transformations are easy with LINQ.
<<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...........
|