Datasheet
Klein c01.tex V3 - 12/13/2007 1:48pm Page 8
Part I: Introduction to Project LINQ
many facets of information, such as in-memory constructs as well as information retrieved from external
sources such as relational data or XML.
These operators provide the capability to express query operations directly and declaratively within
any .NET-based programming language. What makes all of this possible is the simple application of the
query operators to an
IEnumerable
<
T
> source of information.
Found in the
System.Collections.Generic
namespace, the
IEnumerable
<
T
> interface, a new addition
in version 2.0 of the .NET Framework, supports a simple iteration over a collection of a given (specified)
type. The
IEnumerable
<
T
> interface provides a slick mechanism to iterate through an arbitrary collection
of strongly typed objects using the C#
foreach
statement or the Visual Basic
FOR EACH
statement. To
utilize the
foreach
semantics, this interface must be implemented.
So the question is, what does this mean for LINQ? It means that a query that implements this interface can
be a source for the corresponding query expression. You saw several examples of this at the beginning of
this chapter, and the best way to understand the LINQ technology is to see it in action.
The following example utilizes LINQ, a few standard query operators, and the
IEnumerable
<
T
> interface
to query and process the contents within a defined array:
private void ShowLINQ()
{
string[] firstnames = { "Scott", "Steve", "Ken", "Joe", "John",
"Alex", "Chuck", "Sarah"};
IEnumerable
<
string
>
val = from fn in firstnames
where fn.StartsWith("S")
select fn;
foreach (string name in val)
{
Console.WriteLine(name);
}
}
The first statement defines an array of first names. This should not be new to any developer. The next
statement, however, is new. A local variable,
val
in this case, is initialized with a Language Integrated
Query expression. The query expression contains two query operators taken from plethora of
standard query operators. In this example, two operators are used:
where
and
select
.Thelocalvariable
val
exposes the
IEnumerable
<
string
> interface, which provides the capability to iterate through the
collection. The results are actually created as you start to iterate through them via the
foreach
statement.
From here, the query can be modified to add sorting or additional filtering as well as many other options,
but that will be expanded on in later chapters. For now, suffice it to say that via LINQ you can query
various source data types, such as XML and relational data, through a standard and consistent query
model and related query operators.
To illustrate this, let’s modify the directory example from earlier in this chapter. One of the great things
about LINQ is that it enables you easily to ‘‘map’’ object-oriented objects within your .NET programming
language to a database and the objects within a relational database. That means you can access those
relational objects in a strongly typed, object-oriented manner.
8