An Introduction to LINQ
(Page 1 of 4 )
If you're looking for a way to simplify data manipulation in .NET, you may want to check out what LINQ has to offer. It adds some very useful capabilities to .NET. Keep reading for a full overview. This article is the first of two parts.
Programming involves data manipulation; it involves pulling different pieces of information from different sources. One might pull headlines from an RSS feed, query a relational database, or loop over a collection of objects, extracting elements that meet a given condition. Each one of these tasks involves somehow querying a given data source and then turning that data into something useful, but the exact method involved can vary widely. The method may be simple, but, then again, the method may also be more involved, as in querying a database. Still, the overall goal is the same, so why not unify the means to that end?
This is what LINQ, which stands for Language Integrated Query, aims to do. LINQ adds query syntax (similar to SQL, but native) and capabilities to .NET. Using LINQ, one can query a variety of data sources in a unified, easy way. Moreover LINQ contributes to readability, since someone browsing code can easily identify a query and determine what it does. This article will provide an overview of LINQ, its structure and its capabilities, using both C# and VB.NET.
Query Syntax
LINQ adds queries right into the language through new syntax. The syntax is similar to SQL, so it should be familiar to most developers. A query provides the developer with a concise way to say (at the risk of making LINQ seem simplistic), select all the elements where a certain condition is met.
For example, let's say that we have a class, Person, that represents, well, a person, and that for each person, we need to know a name, an age and a phone number. In C#, we can represent this as follows:
class Person
{
string name;
int age;
string phone;
public Person(string name, int age, string phone)
{
this.name = name;
this.age = age;
this.phone = phone;
}
public string Name
{
get
{
return name;
}
}
public int Age
{
get
{
return age;
}
}
public string Phone
{
get
{
return phone;
}
}
Next: VB.NET >>
More .NET Articles
More By Peyton McCullough