LINQ in 60 seconds

Microsoft’s Language INtegrated Query (LINQ) aims to provide a way of selecting objects with a common syntax independent of the data source.

By integrating query into the language instead strings parsed by an external provider at runtime we gain IntelliSense prompting for fields, members and table names and full compile-time syntax checking and a unified syntax.

They will be included in the .NET Framework 3.5 and delivered as part of the Visual Studio 2008 ‘Orcas’ release which is currently available in beta and comprises of:

LINQ syntax

The language extensions themselves. An example in C# might look like:

GridView1.DataSource = from country in countries
                       where country.Continent == 'Europe'
                       orderby population descending
                       select country;

LINQ to Objects

A core part of the .NET Framework 3.5 and allows you to query against any IEnumerable collection and test or sort against any of T’s properties.

In the above example imagine Country is a business class and Countries is a List. Continent is a string property and population a numeric one.

LINQ to SQL (formerly known as DLinq)

LINQ to SQL works by mapping classes and properties to tables and fields as any normal Object-Relational Mapping (ORM) tool would.

It achieves this by marking the classes and properties with attributes to indicate how they map to the underlying database. A visual modeling tool is provided that can generate and manipulate such classes for you from an existing SQL database.

The ORM functionality includes tracking of changed objects and change persistence. It also ensures that you will not obtain multiple objects for the same underlying row in the database (Fowler’s Identity Map).

Other components

There are many other components in the LINQ family including:

LINQ to XML (formerly known as XLinq)

Similar to LINQ to SQL but along the idea of querying XML documents using LINQ syntax rather than the XPath/XQuery syntax.

LINQ to Entities (ADO.NET Entities)

A more advanced ORM solution that allows more extensive mapping and manipulation between how the object appears and the underlying data source.

Third-party support

Mono is actively implementing LINQ as are the people behind the NHibernate ORM.


0 responses