Save Time With LINQ Queries
See how the LINQ syntax, specifically DLinq and XLinq, can increase your productivity and reduce errors.
by Bill Wagner
May 1, 2006
Suppose different departments in your business spoke different languages:
English in software development, French in accounting, German in human resources,
and Spanish in marketing. This scenario would be disastrous for productivity,
regardless of which language was your favorite. So you probably won't encounter
that situation often. Instead, most businesses standardize with a single language
and vocabulary to make communication more efficient.
Software is different. We have created entirely different languages to process
data based on its location. We have C# and VB.NET for general computing. We've
created SQL to modify data that has been stored in a relational database. We've
created XSLT, DOM, and XQuery to process data that has been stored in an XML
document.
Think about this for a minute: The tools you choose for development are based
on the location of the data, not the kind of data you are manipulating. For
example, an Employee object in memory is processed differently from an Employee
record in a database, and you've even got different methods to process the
same Employee information in an XML document. Why?
This problem, a superset of Object Relational Mapping (ORM), is the justification
behind Language Integrated Query (LINQ). The concept behind LINQ is simple:
You can use your favorite general-purpose programming language to manipulate
data regardless of its location.
While this concept is simple, you need to familiarize yourself with the new
technology emerging around it. You need to learn about the language extensions
that support LINQ, DLinq, which translates LINQ queries into code that can
execute in the context of a database; and XLinq, which handles queries and
manipulation of objects in an XML document.
So the LINQ framework developers are still concerned with where data resides,
but you don't have to worry about it. You can write code in your favorite language,
and the LINQ assemblies do the hard work for you. In this article you'll see
how LINQ queries can save you time by simplifying the set of tools you use
to manipulate data.
Language Extensions
To make LINQ technologies work, the C# and VB.NET teams have added many features
to the languages that support querying objects. Here is a simple C# example
that queries an array for numbers divisible by 5:
int [] numbers = ReadNumbers();
var numsDivFive =
from n in numbers
where n % 5 == 0
select n.ToString();
These two statements introduce several new language features, so look at them
one at a time. Start with from n in numbers, which defines an enumeration
across a collection. It more or less has the same meaning as foreach (int
n in numbers). This statement declares a variable that will be used for
each and every element in the collection (n) and the collection being
enumerated (numbers).
Back to top
|