LINQ
   HOME

TheInfoList



OR:

Language Integrated Query (LINQ, pronounced "link") is a
Microsoft Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washi ...
.NET Framework The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
component that adds native data querying capabilities to
.NET languages CLI languages are computer programming languages that are used to produce libraries and programs that conform to the Common Language Infrastructure (CLI) specifications. With some notable exceptions, most CLI languages compile entirely to the Com ...
, originally released as a major part of .NET Framework 3.5 in 2007. LINQ extends the language by the addition of query expressions, which are akin to SQL statements, and can be used to conveniently extract and process data from arrays, enumerable
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differently ...
es,
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. T ...
documents,
relational database A relational database is a (most commonly digital) database based on the relational model of data, as proposed by E. F. Codd in 1970. A system used to maintain relational databases is a relational database management system (RDBMS). Many relati ...
s, and third-party data sources. Other uses, which utilize query expressions as a general framework for readably composing arbitrary computations, include the construction of event handlers or monadic
parsers Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term ''parsing'' comes from L ...
. It also defines a set of method names (called ''standard query operators'', or ''standard sequence operators''), along with translation rules used by the compiler to translate query syntax expressions into expressions using fluent-style (called method syntax by Microsoft) with these method names, lambda expressions and
anonymous type Anonymous types are a feature of C# 3.0, Visual Basic .NET 9.0, Oxygene, Scala and Go that allows data types to encapsulate a set of properties into a single object without having to first explicitly define a type. This is an important feature ...
s. Many of the concepts that LINQ introduced were originally tested in Microsoft's research project. Ports of LINQ exist for PHP
PHPLinq
,
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...

linq.js
, TypeScript
linq.ts
, and
ActionScript ActionScript is an object-oriented programming language originally developed by Macromedia Inc. (later acquired by Adobe). It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript (meaning ...

ActionLinq
, although none are strictly equivalent to LINQ in the .NET inspired languages C#, F# and VB.NET (where it is a part of the language, not an external library, and where it often addresses a wider range of needs).


Architecture of LINQ in the .NET Framework


Standard Query Operator API

In what follows, the descriptions of the operators are based on the application of working with collections. Many of the operators take other functions as arguments. These functions may be supplied in the form of a named method or anonymous function. The set of query operators defined by LINQ is exposed to the user as the Standard Query Operator (SQO) API. The query operators supported by the API are: ;Select: The Select operator performs a projection on the collection to select interesting aspects of the elements. The user supplies an arbitrary function, in the form of a named or lambda expression, which projects the data members. The function is passed to the operator as a delegate. ;Where: The Where operator allows the definition of a set of predicate rules that are evaluated for each object in the collection, while objects that do not match the rule are filtered away. The predicate is supplied to the operator as a delegate. ;SelectMany: For a user-provided mapping from collection elements to collections, semantically two steps are performed. First, every element is mapped to its corresponding collection. Second, the result of the first step is flattened by one level. ''Note:'' Select and Where are both implementable in terms of SelectMany, as long as singleton and empty collections are available. The translation rules mentioned above still make it mandatory for a LINQ provider to provide the other two operators. ;Sum / Min / Max / Average: These operators optionally take a function that retrieves a certain numeric value from each element in the collection and uses it to find the sum, minimum, maximum or average values of all the elements in the collection, respectively. Overloaded versions take no function and act as if the identity is given as the lambda. ;Aggregate: A generalized Sum / Min / Max. This operator takes a function that specifies how two values are combined to form an intermediate or the final result. Optionally, a starting value can be supplied, enabling the result type of the aggregation to be arbitrary. Furthermore, a finalization function, taking the aggregation result to yet another value, can be supplied. ;Join / GroupJoin: The Join operator performs an inner join on two collections, based on matching keys for objects in each collection. It takes two functions as delegates, one for each collection, that it executes on each object in the collection to extract the key from the object. It also takes another delegate in which the user specifies which data elements, from the two matched elements, should be used to create the resultant object. The GroupJoin operator performs a
group join A join clause in SQL – corresponding to a join operation in relational algebra – combines columns from one or more tables into a new table. Informally, a join stitches two tables and puts on the same row records with matching fields : INNER, ...
. Like the Select operator, the results of a join are instantiations of a different class, with all the data members of both the types of the source objects, or a subset of them. ;Take / TakeWhile: The Take operator selects the first n objects from a collection, while the TakeWhile operator, which takes a predicate, selects those objects that match the predicate (stopping at the first object that doesn't match it). ;Skip / SkipWhile: The Skip and SkipWhile operators are complements of Take and TakeWhile - they skip the first n objects from a collection, or those objects that match a predicate (for the case of SkipWhile). ;OfType: The OfType operator is used to select the elements of a certain type. ;Concat: The Concat operator concatenates two collections. ;OrderBy / ThenBy: The OrderBy operator is used to specify the primary sort ordering of the elements in a collection according to some key. The default ordering is in ascending order, to reverse the order, the OrderByDescending operator is to be used. ThenBy and ThenByDescending specifies subsequent ordering of the elements. The function to extract the key value from the object is specified by the user as a delegate. ;Reverse: The Reverse operator reverses a collection. ;GroupBy: The GroupBy operator takes a function that extracts a key value and returns a collection of IGrouping objects, for each distinct key value. The IGrouping objects can then be used to enumerate all the objects for a particular key value. ;Distinct: The Distinct operator removes duplicate instances of an object from a collection. An overload of the operator takes an equality comparer object which defines the criteria for distinctness. ;Union / Intersect / Except: These operators are used to perform a union,
intersection In mathematics, the intersection of two or more objects is another object consisting of everything that is contained in all of the objects simultaneously. For example, in Euclidean geometry, when two lines in a plane are not parallel, thei ...
and difference operation on two sequences, respectively. Each has an overload which takes an equality comparer object which defines the criteria for element equality. ;SequenceEqual: The SequenceEqual operator determines whether all elements in two collections are equal and in the same order. ;First / FirstOrDefault / Last / LastOrDefault: These operators take a predicate. The First operator returns the first element for which the predicate yields true, or, if nothing matches, throws an exception. The FirstOrDefault operator is like the First operator except that it returns the default value for the element type (usually a null reference) in case nothing matches the predicate. The last operator retrieves the last element to match the predicate, or throws an exception in case nothing matches. The LastOrDefault returns the default element value if nothing matches. ;Single: The Single operator takes a predicate and returns the element that matches the predicate. An exception is thrown, if none or more than one element match the predicate. ;SingleOrDefault: The SingleOrDefault operator takes a predicate and return the element that matches the predicate. If more than one element matches the predicate, an exception is thrown. If no element matches the predicate, a default value is returned. ;ElementAt: The ElementAt operator retrieves the element at a given index in the collection. ;Any / All: The Any operator checks, if there are any elements in the collection matching the predicate. It does not select the element, but returns true if at least one element is matched. An invocation of any without a predicate returns true if the collection non-empty. The All operator returns true if all elements match the predicate. ;Contains: The Contains operator checks, if the collection contains a given element. ;Count: The Count operator counts the number of elements in the given collection. An overload taking a predicate, counts the number of elements matching the predicate. The Standard Query Operator API also specifies certain operators that convert a collection into another type: * AsEnumerable: Statically types the collection as an IEnumerable. * AsQueryable: Statically types the collection as an IQueryable. * ToArray: Creates an array T[] from the collection. * ToList: Creates a List from the collection. * ToDictionary: Creates a Dictionary from the collection, indexed by the key K. A user supplied projection function extracts a key from each element. * ToLookup: Creates a Lookup from the collection, indexed by the key K. A user supplied projection function extracts a key from each element. * Cast: converts a non-generic IEnumerable collection to one of IEnumerable by casting each element to type T. Alternately converts a generic IEnumerable to another generic IEnumerable by casting each element from type T to type R. Throws an exception in any element cannot be cast to the indicated type. * OfType: converts a non-generic IEnumerable collection to one of IEnumerable. Alternately converts a generic IEnumerable to another generic IEnumerable by attempting to cast each element from type T to type R. In both cases, only the subset of elements successfully cast to the target type are included. No exceptions are thrown.


Language extensions

While LINQ is primarily implemented as a
library A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vi ...
for .NET Framework 3.5, it also defines optional language extensions that make queries a first-class language construct and provide syntactic sugar for writing queries. These language extensions have initially been implemented in C# 3.0, VB 9.0, F# and Oxygene, with other languages like Nemerle having announced preliminary support. The language extensions include: *Query syntax: A language is free to choose a query syntax that it will recognize natively. These language keywords must be translated by the compiler to appropriate LINQ method calls. *Implicitly typed variables: This enhancement allows variables to be declared without specifying their types. The languages C# 3.0 and Oxygene declare them with the var keyword. In VB9.0, the Dim keyword without type declaration accomplishes the same. Such objects are still
strongly typed In computer programming, one of the many ways that programming languages are colloquially classified is whether the language's type system makes it strongly typed or weakly typed (loosely typed). However, there is no precise technical definition ...
; for these objects the compiler infers the types of variables via
type inference Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistic ...
, which allows the results of the queries to be specified and defined without declaring the type of the intermediate variables. *
Anonymous type Anonymous types are a feature of C# 3.0, Visual Basic .NET 9.0, Oxygene, Scala and Go that allows data types to encapsulate a set of properties into a single object without having to first explicitly define a type. This is an important feature ...
s: Anonymous types allow classes that contain only data-member declarations to be inferred by the compiler. This is useful for the Select and Join operators, whose result types may differ from the types of the original objects. The compiler uses type inference to determine the fields contained in the classes and generates accessors and mutators for these fields. * Object Initializer: Object initializers allow an object to be created and initialized in a single scope, as required for Select and Join operators. * Lambda expressions: Lambda expressions allow predicates and other projection functions to be written inline with a concise syntax, and support full lexical closure. They are captured into parameters as delegates or expression trees depending on the Query Provider. For example, in the query to select all the objects in a collection with SomeProperty less than 10, var results = from c in SomeCollection where c.SomeProperty < 10 select new ; foreach (var result in results) the types of variables ''result'', ''c'' and ''results'' all are inferred by the compiler in accordance to the signatures of the methods eventually used. The basis for choosing the methods is formed by the query expression-free translation result var results = SomeCollection .Where(c => c.SomeProperty < 10) .Select(c => new ); results.ForEach(x => )


LINQ providers

The C#3.0 specification defines a Query Expression Pattern along with translation rules from a LINQ expression to an expression in a subset of C# 3.0 without LINQ expressions. The translation thus defined is actually un-typed, which, in addition to lambda expressions being interpretable as either delegates or expression trees, allows for a great degree of flexibility for libraries wishing to expose parts of their interface as LINQ expression clauses. For example, LINQ to Objects works on IEnumerables and with delegates, whereas LINQ to SQL makes use of the expression trees. The expression trees are at the core of the LINQ extensibility mechanism, by which LINQ can be adapted for many data sources. The expression trees are handed over to LINQ Providers, which are data source-specific implementations that adapt the LINQ queries to be used with the data source. If they choose so, the LINQ Providers analyze the expression trees contained in a query in order to generate essential pieces needed for the execution of a query. This can be SQL fragments or any other completely different representation of code as further manipulatable data. LINQ comes with LINQ Providers for in-memory object collections,
Microsoft SQL Server Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which ...
databases, ADO.NET datasets and XML documents. These different providers define the different flavors of LINQ:


LINQ to Objects

The LINQ to Objects provider is used for in-memory collections, using the local query execution engine of LINQ. The code generated by this provider refers to the implementation of the standard query operators as defined on the Sequence pattern and allows IEnumerable collections to be queried locally. Current implementation of LINQ to Objects perform interface implementation checks to allow for fast membership tests, counts, and indexed lookup operations when they are supported by the runtime type of the IEnumerable.


LINQ to XML (formerly called XLINQ)

The LINQ to XML provider converts an XML document to a collection of XElement objects, which are then queried against using the local execution engine that is provided as a part of the implementation of the standard query operator.


LINQ to SQL (formerly called DLINQ)

The LINQ to SQL provider allows LINQ to be used to query
Microsoft SQL Server Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which ...
databases, including SQL Server Compact databases. Since SQL Server data may reside on a remote server, and because SQL Server has its own query engine, LINQ to SQL does not use the query engine of LINQ. Instead, it converts a LINQ query to a SQL query that is then sent to SQL Server for processing. However, since SQL Server stores the data as relational data and LINQ works with data encapsulated in objects, the two representations must be mapped to one another. For this reason, LINQ to SQL also defines a mapping framework. The mapping is done by defining classes that correspond to the tables in the database, and containing all or a subset of the columns in the table as data members. The correspondence, along with other
relational model The relational model (RM) is an approach to managing data using a structure and language consistent with first-order predicate logic, first described in 1969 by English computer scientist Edgar F. Codd, where all data is represented in terms of t ...
attributes such as
primary key In the relational model of databases, a primary key is a ''specific choice'' of a ''minimal'' set of attributes ( columns) that uniquely specify a tuple ( row) in a relation ( table). Informally, a primary key is "which attributes identify a recor ...
s, are specified using LINQ to SQL-defined attributes. For example, able(Name="Customers")public class Customer This class definition maps to a table named Customers and the two data members correspond to two columns. The classes must be defined before LINQ to SQL can be used.
Visual Studio 2008 Visual Studio is an integrated development environment (IDE) from Microsoft. It is used to develop computer programs including web site, websites, web apps, web services and mobile apps. Visual Studio uses Microsoft software development platfor ...
includes a mapping designer that can be used to create the mapping between the data schemas in the object as well as the relational domain. It can automatically create the corresponding classes from a
database schema The database schema is the structure of a database described in a formal language supported by the database management system (DBMS). The term "schema" refers to the organization of data as a blueprint of how the database is constructed (divi ...
, as well as allow manual editing to create a different view by using only a subset of the tables or columns in a table. The mapping is implemented by the DataContext that takes a connection string to the server, and can be used to generate a Table where T is the type to which the database table will be mapped. The Table encapsulates the data in the table, and implements the IQueryable interface, so that the expression tree is created, which the LINQ to SQL provider handles. It converts the query into
T-SQL Transact-SQL (T-SQL) is Microsoft's and Sybase's proprietary extension to the SQL (Structured Query Language) used to interact with relational databases. T-SQL expands on the SQL standard to include procedural programming, local variables, v ...
and retrieves the result set from the database server. Since the processing happens at the database server, local methods, which are not defined as a part of the lambda expressions representing the predicates, cannot be used. However, it can use the
stored procedure A stored procedure (also termed proc, storp, sproc, StoPro, StoredProc, StoreProc, sp, or SP) is a subroutine available to applications that access a relational database management system (RDBMS). Such procedures are stored in the database data d ...
s on the server. Any changes to the result set are tracked and can be submitted back to the database server.


LINQ to DataSets

Since the LINQ to SQL provider (above) works only with
Microsoft SQL Server Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which ...
databases, in order to support any generic database, LINQ also includes the LINQ to DataSets. It uses ADO.NET to handle the communication with the database. Once the data is in ADO.NET Datasets, LINQ to DataSets execute queries against these datasets.


Performance

Non-professional users may struggle with subtleties in the LINQ to Objects features and syntax. Naive LINQ implementation patterns can lead to a catastrophic degradation of performance. LINQ to XML and LINQ to SQL performance compared to ADO.NET depends on the use case.


PLINQ

Version 4 of the .NET framework includes PLINQ, or ''Parallel LINQ'', a
parallel Parallel is a geometric term of location which may refer to: Computing * Parallel algorithm * Parallel computing * Parallel metaheuristic * Parallel (software), a UNIX utility for running programs in parallel * Parallel Sysplex, a cluster o ...
execution engine for LINQ queries. It defines the ParallelQuery class. Any implementation of the IEnumerable interface can take advantage of the PLINQ engine by calling the AsParallel(this IEnumerable) extension method defined by the ParallelEnumerable class in the System.Linq namespace of the .NET framework. The PLINQ engine can execute parts of a query concurrently on multiple threads, providing faster results.


See also

* Object-relational mapping (ORM) * Object-relational impedance mismatch * List comprehension * Lazy evaluation


References


External links


Official Microsoft LINQ Project

LINQ to Objects for the .NET developer

Future of LINQ to SQL

How does it work in C#? - Part 3 (C# LINQ in detail)
{{Query languages .NET terminology Query languages XML data access