Introduction to LINQ

What Is LINQ?
LINQ is a new feature of C# and Visual Basic .NET that integrates into these languages the ability
to query data. In a relational database system, data is organized into nicely normalized tables, and
accessed with a very simple but powerful query language⎯SQL. SQL can work with any set
of data in a database because the data is organized into tables, following strict rules.
In a program, as opposed to a database, however, data is stored in class objects or structs
that are all vastly different. As a result, there has been no general query language for retrieving
data from data structures. The method of retrieving data from objects has always been customdesigned
as part of the program. With the introduction of LINQ in C# 3.0, however, the ability
to query collections of objects has been added to the language. The following are the important
high-level characteristics of LINQ:
• LINQ (pronounced link) stands for Language Integrated Query.
• LINQ is an extension of the .NET Framework that allows you to query collections of data
in a manner similar to database queries.
• C# 3.0 includes extensions that integrate LINQ into the language, allowing you to query
data from databases, collections of program objects, and XML documents.
The following code shows a simple example of using LINQ. In this code, the data source
being queried is simply an array of ints. The definition of the query is the statement with the
from and select keywords. Although the query is defined in this statement, it is actually performed
and used in the foreach statement at the bottom.

static void Main()
int[] numbers = { 2, 12, 5, 15 }; // Data source
IEnumerable<int> lowNums = // Define and store the query.
from n in numbers
where n < 10
select n;
foreach (var x in lowNums) // Execute the query.
Console.Write("{0}, ", x);

This code produces the following output:
2, 5,

LINQ Providers
In the above example, the data source was simply an array of ints, which is an in-memory
object of the program. LINQ, however, can work with many different types of data sources,
such as SQL databases, XML documents, and a host of others. For every data source type, however,
under the covers there must be a module of code that implements the LINQ queries in
terms of that data source type. These code modules are called LINQ providers. The important
points about LINQ providers are the following:
• Microsoft provides LINQ providers for a number of common data source types
• You can use any LINQ-enabled language to query any data source type for which there is a LINQ provider.

The architecture of LINQ, the LINQ-enabled languages, and LINQ providers



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s