The buzz around .NET Language-Integrated Query (LINQ) is hard to ignore, especially since Microsoft is embracing it with the latest release of the .NET platform (beginning with C# 3.0 and Visual Basic 9.0) and Visual Studio 2008.

LINQ basically provides data access via the developer’s language of choice without using SQL. Within the context of the .NET platform, LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. Presently, C# and Visual Basic are extended with native language syntax via class libraries for working with data.

The key term in the LINQ name is integrated, as it applies to developers being able to interact with data sources via their own development language with C# and Visual Basic currently supported. The integration negates the need to be familiar with SQL, so you can use your favorite programming language when working with backend data.

LINQ was designed to work with any backend data source, and developers benefit from the many features of their IDE, such as Visual Studio 2008. This brings compile-time syntax checking, static typing, and code assist to code accessing external data — features that are sorely missed when working with external tools like SQL Server Enterprise Manager.

Microsoft divides LINQ into three areas:

  • LINQ to Object: In-memory data is queried and manipulated.
  • LINQ to ADO.NET: This includes three variations: LINQ to SQL (working with relational data, with only SQL Server supported) provides an object relational mapping implementation, LINQ to DataSet (working with ADO.NET datasets), and LINQ to Entities (Microsoft ORM solution).
  • LINQ to XML: XML data sources are queried and manipulated.

These three areas of LINQ functionality are provided via various .NET class libraries.

Class libraries

LINQ functionality is provided via its own set of classes. The main LINQ namespace is System.Linq, which provides classes and interfaces that support queries that use LINQ. The following namespaces are also provided:

  • System.Linq.Expressions: Contains classes, interfaces, and enumerations that enable language-level code expressions to be represented as objects in the form of expression trees.
  • System.Data.Linq: Provides classes that facilitate LINQ to relational databases (LINQ to SQL).
  • System.Data.Linq.Mapping: Works with the System.Data.Linq namespace for working with relational database systems. It allows you to generate an object model that represents the structure and content of the data source.
  • System.Data.Linq.SqlClient: Contains provider classes for communicating with SQL Server and classes that contain query helper methods. This namespace is not intended for direct use in code.
  • System.Xml.Linq: Contains the classes for working with XML data sources (LINQ to XML). These classes allow you to do things such as load XML data from files or streams as well as query, validate, and manipulate XML trees. LINQ to XML is an in-memory XML programming interface that enables you to modify XML documents efficiently and easily.

A key facet of LINQ is that it provides complete query capabilities for any collection that implements IEnumerable or IQueryable. For in-memory objects to be enabled for LINQ, the object type should implement IEnumerable. For LINQ to XML and LINQ to ADO.NET, the underlying provider converts the target objects to IEnumerable-based collections.


The fact that LINQ works with all collections that use the IEnumerable interface means the cost of entry to using LINQ is low because almost all .NET type containers support IEnumerable. The following C# sample provides a demonstration of using LINQ to work with an array of string values. An IEnumerable string is used to loop through all word values in the string array.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace LINQConsoleTest {

class Program {

static void Main(string[] args) {

string[] words = new string[3] { "", "", ""};

IEnumerable<string> wordQuery =

from word in words

select word;

foreach (string wrd in wordQuery) {



While working with string data is good, SQL Server interaction will be more popular. Visual Studio 2008 includes a designer tool that provides a simple way to model and visualize a database as a LINQ to SQL data model. Database elements may be dragged and dropped into the designer environment; the result is a dbml file. The dbml file generates the class file for data operations like Create, read, update, and delete.

Once you drag and drop various database features to the designer, a name is assigned to the dbml file; this name is used to interact with its underlying data source. As an example, I created a connection to the employee table of the SQL Server Northwind database. The following C# code accesses the data source:

NorthwindDataContext db = new NorthwindDataContext();

var employers = from p in db.Employees

select p;

The equivalent Visual Basic code follows:

Dim db As New NorthwindDataContext
Dim employees = From p in db.Employees
select p;

I will provide more code details in next week’s article.

Learning with LINQPad

When tackling a new technology, I prefer to jump in and get my hands dirty. With LINQ, you can do this by firing up Visual Studio 2008 and creating a new project that uses LINQ to connect to a data source. Also, a free tool called LINQPad provides an easy to use interface for working with LINQ functionality.

LINQPad is available as a free download. It is ready to use once the executable is downloaded. It comes with plenty of example code and allows you to easily work with LINQ by creating connections and interacting with the data source by dragging and dropping to a source pane or directly typing code.

Focus on the code

Microsoft has repeatedly vowed to reduce the workload of application developers by streamlining certain processes within its development platforms and tools. LINQ actually does simplify data access by allowing a developer to interact with backend data sources using their familiar programming language as opposed to using venerable SQL.

Have you had a chance to work with LINQ in the .NET Framework? Do you see advantages to negating the need of developers to learn and use SQL? Share your thoughts and experiences with the Visual Studio Developer community.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.


Get weekly .NET tips in your inbox
TechRepublic’s free Visual Studio Developer newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically subscribe today!