Language Integrated Query

From Wikipedia, the free encyclopedia

Language Integrated Query (LINQ) is a Microsoft project that adds a native querying syntax reminiscent of SQL to .NET Framework programming languages, initially to the C# and Visual Basic .NET languages.

LINQ defines standard query operators that allow code written in LINQ-enabled languages to filter, enumerate, and create projections of several types of collections using the same syntax. Such collections may include arrays, enumerable classes, XML, datasets from relational databases, and third party data sources. The LINQ project uses features of version 2.0 of the .NET Framework, new LINQ-related assemblies, and extensions to the C# and Visual Basic .NET languages. Microsoft has distributed a preview release of LINQ, consisting of those libraries and compilers for C# 3.0 and Visual Basic 9. Other languages, such as F# and Nemerle, have also announced preliminary support.

Contents

[edit] Language features

LINQ uses several new language features to expose query syntax natively to languages such as C#:

[edit] Data sources

Although LINQ initially supports queries of in-memory collections, relational databases and XML data, it is an extensible architecture that allows third-party providers of additional data sources to expose their data sources for use with LINQ by implementing the Standard Query Operators as Extension methods for those data sources.

For example, LINQ to SQL (formerly DLinq), which translates LINQ expressions into SQL database queries, makes use of the compiler's ability to build an expression tree from source based on context, rather than building a function delegate. Given the expression tree describing the query, a database-specific provider can analyze and transform it into the appropriate query language for the data store, such as Microsoft SQL Server, Jet (used by Microsoft Access), or third-party database servers. Some individuals have already created proof-of-concept LINQ libraries for querying WMI, RSS, ADO.NET Dataset objects, and Amazon Web Services using similar tactics.

The existing preview from Microsoft also includes an implementation of LINQ to XML (formerly XLinq), which greatly simplifies XML document construction and querying, using similar patterns.


[edit] SQLMetal

LINQ framework includes a tool named SQLMetal which allows automatic generation of classes directly from a MS-SQL database, allowing very fast and easy integration of code and database.

[edit] Example

// the Northwind type is a subclass of DataContext created by SQLMetal
// Northwind.Orders is of type Table<Order>
// Northwind.Customers is of type Table<Customer>

Northwind db = new Northwind(connectionString);

// use 'var' keyword because there is no name for the resultant type of the projection

var q = from o in db.Orders, c in db.Customers
    where o.Quality == "200" && (o.CustomerID == c.CustomerID)
    select new { o.DueDate, c.CompanyName, c.ItemID, c.ItemName };

// q is now an IEnumerable<T>, where T is the anonymous type generated by the compiler

foreach (var t in q)
{
    // t is strongly typed, even if we can't name the type at design time

    Console.WriteLine("DueDate Type = {0}", t.DueDate.GetType());
    Console.WriteLine("CompanyName (lowercased) = {0}", t.CompanyName.ToLower());
    Console.WriteLine("ItemID * 2 = {0}", t.ItemID * 2);
}

[edit] Release date

LINQ is planned for release with the 'Orcas' version of Visual Studio, probably some time in 2007, after the Windows Vista cycle.[1]

[edit] See also

In other languages