Fact Checked

What is a Language Integrated Query?

Jessica Seminara
Jessica Seminara

Language integrated query (LINQ) provides the ability to seamlessly integrate declarative query syntax into imperative languages, primarily C# and VB.NET. The development of LINQ solved the problems developers working in object-oriented environments encountered when performing queries. It was introduced as part of the Microsoft® .NET Framework version 3.5 in November 2007, and in Visual Studio® codename "Orcas."

Prior to the creation of language integrated query, .NET languages had no native way to query relational databases or XML documents. Developers writing in these languages had to embed SQL or XQuery respectively as strings, and manually map resulting data to .NET objects. Embedded query strings and mappings could not be validated by .NET compilers, allowing potential errors to be discovered at run time.

Man holding computer
Man holding computer

Another problem developers faced before language integrated query was that .NET languages had only objected-oriented syntax to query collections of .NET objects. Typically, the developer would iterate over a whole source collection, building new result collections based on search criteria. The developer could also transform the data while building the result collections. This approach was cumbersome; a simple select query written in C# or VB would require at least five lines of code, whereas the same query would require one line of code in a query language.

The challenges of querying within an object-oriented environment gave Microsoft the impetus to create LINQ. LINQ provides a query syntax reminiscent of SQL to be used in-line with C# or VB, complete with static type checking. When querying .NET collections, instead of using nested loops, developers can use LINQ syntax to select items and transform data. Among other advantages, this results in fewer lines of code as compared to purely object-oriented syntax.

When accessing external data sources, LINQ and supporting components remove the need for embedded query languages and data mappings within a .NET language itself. In order to perform data mapping, LINQ depends on components called LINQ providers. LINQ-to-Objects is the stock provider that enables LINQ to access .NET collections. Similarly, there are providers to access relational databases and XML documents. Most of these providers are third-party components not included in the .NET Framework. Regardless of the provider used, all queries use the same LINQ syntax.

Between the language integrated query syntax and the providers is a library of query operators added to the .NET Framework as a part of the LINQ facilities. These operators perform such tasks as filtering, ordering, set theory operations, and aggregation. As these operators are part of the .NET Framework, they can also be used without LINQ if the developer so chooses.

You might also Like

Discuss this Article

Post your comments
Forgot password?
    • Man holding computer
      Man holding computer