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 favourite programming language when working with back-end data.

LINQ was designed to work with any back-end 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.

IEnumerable

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] { "TechRepublic.com", "CNET.com", "News.com"};
    IEnumerable<string> wordQuery = from word in words
    select word;
    foreach (string wrd in wordQuery) {
      Console.WriteLine(wrd);
      }
    }
  }
}

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 visualise 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 back-end 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.

Advertisement

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Related links

Comments

1

Leigh - 15/05/08

Nice Idea, but if a developer isn't able to come to grips with writing SQL, then he isn't going to be able to come to grips with working with data in any other language.

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

Leigh - 15/05/08

Nice Idea, but if a developer isn't able to come to grips with writing SQL, then he isn't going to be ... more

Log in


Sign up | Forgot your password?

  • Lana Kovacevic What's new in GWT 1.5?

    I recently wrote an introduction to the Google Web Toolkit based on Lars Rasmussen's session at the Google Developer Day 2008 in Sydney. Following the introductory session Lars gave us a deeper insight into GWT, particularly what's new in version 1.5. Read more »

    -- posted by Lana Kovacevic

  • Lana Kovacevic The Portal of the Future

    At this year's Gartner Application Development, Integration and Web Services Summit, I attended Gene Phifer talk: "Portal of the Future: What's Beyond Web 2.0?". Read more »

    -- posted by Lana Kovacevic

  • Staff Google's new foray into image search

    Google is developing visual crawling software that can be used for facial recognition and scene analysis. In addition images can be matched with display ads and utilise geotagging information for various applications. Read more »

    -- posted by Staff

What's on?

  • Club Builder: Sports, Gates and Gears

    This week on Club Builder: Steve Ballmer gives a teary goodbye to Bill Gates, Mark Taylor moves into IT endorsements and we ask some Google Gears questions.