Datasheet
Klein c01.tex V3 - 12/13/2007 1:48pm Page 7
Chapter 1: Project LINQ
var conQuery =
from c in contact
where c.FirstName.StartsWith("S")
orderby c.LastName
select new { c.FirstName, c.LastName, c.EmailAddress} ;
foreach (var item in conQuery)
ListBox1.Items.Add(item.FirstName + " " +
item.LastName + " " +
item.EmailAddress);
This previous example queries the
Person.Contact
table in the AdventureWorks database for all contacts
whose first name starts with the letter ‘‘S’’.
The purpose of LINQ is to provide developers with the following benefits:
❑ A simplified way to write queries.
❑ Faster development time by removing run-time errors and catching errors at compile time.
❑ IntelliSense and debugging support for LINQ directly in the development language.
❑ Closing the gap between relational data and object-oriented development.
❑ A unified query syntax to use regardless of the source of data.
What is important to notice is the same syntax that you used to query the system processes was used
query a SQL data source. Both of these topics will be discussed in much more detail, including how to
easily connect and map to the source database.
So, with that primer, this chapter introduces the following topics:
❑ LINQ
❑ LINQ to XML
❑ LINQ to SQL
LINQ Overview
LINQ is a set of standard query operators that brings powerful query facilities right into the .NET
Framework language such as C# and VB.NET. The LINQ framework brings together the capability of
data access with the power of data manipulation. This section provides an overview of the
capabilities of LINQ and the standard query operators, but Chapters 3 and 4, respectively, will discuss in
great detail the LINQ query operators and language features that contribute to LINQ’s direct, declarative
style of queries.
The term Language Integrated Query signifies that the standard query facilities are architected directly
into the developer’s .NET-supported programming language of choice. These query facilities, known
as the standard query operators, expose general-purpose query mechanisms that can be applied to
7