Datasheet
Klein c01.tex V3 - 12/13/2007 1:48pm Page 14
Part I: Introduction to Project LINQ
This isn’t to say that I only have friends whose first names begin with the letter S, but you get the idea.
This query returns a sequence of
XElements
containing the names of those whose first name begins with
the letter S. The data comes not from a self-generated XML document but an outside source, in this
case the array of first names. However, the data could just as easily come from a relational database or
even another XML document.
What
XElement
enables you to do is query non-XML sources and produce XML results via the utilization
of the
XElements
in the body of the
select
clause, as shown earlier. Gnarly.
The object of these simple examples is to illustrate the basic concepts of LINQ to XML and the great
power, flexibility, and ease with which XML can be manipulated. Note that the same standard query
operators were used to generate the XML document in this example as in the first one. Nothing had to
be changed in the query really, other than using the types to help integrate LINQ with XML to build the
resulting XML. Yet the query operators remained the same, as did the overall syntax of the query expres-
sion. This way is much better than trying to figure out XQuery or XPath, working
with the DOM or even XSLT. Chapters 10 through 13 cover LINQ to XML in much
greater detail.
LINQ to SQL Overview
LINQ to SQL, or DLINQ, is another component in the LINQ technology ‘‘utility belt.’’ It provides a
mechanism for managing relational data via a run-time infrastructure. The great thing about this is that
LINQ still keeps its strong points, such as the ability to query. This is accomplished by translating the
Language Integrated Query into SQL syntax for execution on the database server. Once the query has
been executed, the tabular results are handed back to the client in the form of objects that you as a
developer have defined.
If you have been following the LINQ talk, you already know that LINQ to SQL is the next version of
ADO.NET. This is great news, and by the time you are done with this section and the section on LINQ
to SQL later in the book, you will surely know why. LINQ takes advantage of the information produced
by the SQL schema and integrates this information directly into the CLR (Common Language Runtime)
metadata. Because of this integration, the definitions of the SQL tables and views are compiled into CLR
types, making them directly accessible from within your programming language.
For example, the following defines a simple schema based on the
Person.Contact
table from the
AdventureWorks database:
[Table(Name="Person.Contact")]
public class Contact
{
[Column(DBType = "nvarchar(50) not null")]
public string FirstName;
[Column(DBType = "nvarchar(50) not null")]
public string LastName;
[Column(DBType = "nvarchar(50) not null")]
public string EmailAddress;
}
14