|
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>>
|
Integrated
LINQ stands for Language Integrated Query. One of the central, and most
important, features of LINQ is its integration of a flexible query syntax into
the C# language.
Developers have many tools that have been crafted to neatly solve dif- ficult
tasks. Yet there are still dark corners in the development landscape. Querying
data is one area in which developers frequently encounter problems with no
clear resolution. LINQ aims to remove that uncertainty and to show a clearly
defined path that is well-lit and easy to follow.
In Visual Studio 2005, attempts to query data in a SQL database from a C#
program revealed an impedance mismatch between code and data. SQL is native to
neither .NET nor C#. As a result, SQL code embedded in a C# program is neither
type-checked nor IntelliSense-aware. From the perspective of a C# developer,
SQL is shrouded in darkness.
Here is an example of one of several different techniques developers used in the
past when querying data:
Of these six lines of code, only the last two directly define a query. The rest
of the lines involve setup code that allows developers to connect and call
objects in the database. The query string shown in the next-to-last line is
neither type-checked nor IntelliSense-aware.
After these six lines of code execute, the developers may have more work to do,
because the data returned from the query is not readily addressable by an
object-oriented programmer. You might have to write more lines of code to
access this data, or convert it into a format that is easier to use.
The LINQ version of this same query is shorter, easier to read, colorcoded,
fully type-checked, and IntelliSense-aware. The result set is cleanly converted
into a well-defined object-oriented format:
By fully integrating the syntax for querying data into .NET languages such as C#
and VB, LINQ resolves a problem that has long plagued the development world.
Queries become first-class citizens of our primary languages; they are both
type-checked and supported by the powerful IntelliSense technology provided
inside the Visual Studio IDE. LINQ brings the experience of writing queries
into the well-lit world of the 21st century.
A few benefits accrue automatically as a result of integrating querying into the
C# language:
-
The syntax highlighting and IntelliSense support allow you to get more work
done in less time. The Visual Studio editor automatically shows you the tables
in your database, the correctly spelled names and types of your fields, and the
operators you can use when querying data. This helps you save time and avoid
careless mistakes.
-
LINQ code is shorter and cleaner than traditional techniques for querying data
and, therefore, is much easier to maintain.
-
LINQ allows you to fully harness the power of your C# debugger while writing
and maintaining queries. You can step through your queries and related code in
your LINQ projects.
If language integration were the only feature that LINQ offered, that alone
would have been a significant accomplishment. But we are only oneseventh of the
way through our description of the foundations of LINQ. Many of the best and
most important features are still to be covered.
<<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...........
|