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:3
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.
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. This implement the Fold higher-order function.
The standard query operator API also specifies certain operators that convert a collection into another type:4
While LINQ is primarily implemented as a library 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,6: 75 VB 9.0, F#7 and Oxygene, with other languages like Nemerle having announced preliminary support. The language extensions include:8
For example, in the query to select all the objects in a collection with SomeProperty less than 10,
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
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 IEnumerable<T>s 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 databases, ADO.NET datasets and XML documents. These different providers define the different flavors of LINQ:
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<T> 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.101112
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.13
The LINQ to SQL provider allows LINQ to be used to query Microsoft SQL Server 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.14 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.15 The correspondence, along with other relational model attributes such as primary keys, are specified using LINQ to SQL-defined attributes. For example,
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 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, as well as allow manual editing to create a different view by using only a subset of the tables or columns in a table.16
The mapping is implemented by the DataContext that takes a connection string to the server, and can be used to generate a Table<T> where T is the type to which the database table will be mapped. The Table<T> encapsulates the data in the table, and implements the IQueryable<T> interface, so that the expression tree is created, which the LINQ to SQL provider handles. It converts the query into T-SQL 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 procedures on the server. Any changes to the result set are tracked and can be submitted back to the database server.17
Since the LINQ to SQL provider (above) works only with Microsoft SQL Server 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.18
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.1920
LINQ to XML and LINQ to SQL performance compared to ADO.NET depends on the use case.2122
Version 4 of the .NET framework includes PLINQ, or Parallel LINQ, a parallel execution engine for LINQ queries. It defines the ParallelQuery<T> class. Any implementation of the IEnumerable<T> interface can take advantage of the PLINQ engine by calling the AsParallel<T>(this IEnumerable<T>) extension method defined by the ParallelEnumerable class in the System.Linq namespace of the .NET framework.23 The PLINQ engine can execute parts of a query concurrently on multiple threads, providing faster results.24
Many of the concepts that LINQ introduced were originally tested in Microsoft's Cω research project, formerly known by the codenames X# (X Sharp) and Xen. It was renamed to Cω after Polyphonic C# (another research language based on join calculus principles) was integrated into it.
Cω attempts to make datastores (such as databases and XML documents) accessible with the same ease and type safety as traditional types like strings and arrays. Many of these ideas were inherited from an earlier incubation project within the WebData XML team called X# and Xen. Cω also includes new constructs to support concurrent programming; these features were largely derived from the earlier Polyphonic C# project.25
First available in 2004 as a compiler preview, Cω's features were subsequently used by Microsoft in the creation of the LINQ features released in 2007 in .NET version 3.526 The concurrency constructs have also been released in a slightly modified form as a library, named Joins Concurrency Library, for C# and other .NET languages by Microsoft Research.27
Ports of LINQ exist for PHP (PHPLinq Archived 2018-01-19 at the Wayback Machine), JavaScript (linq.js), TypeScript (linq.ts), and ActionScript (ActionLinq Archived 2018-12-25 at the Wayback Machine), 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).
"Rx framework". 10 June 2011. http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx ↩
"Monadic Parser Combinators using C#3". Retrieved 2009-11-21. http://blogs.msdn.com/lukeh/archive/2007/08/19/monadic-parser-combinators-using-c-3-0.aspx ↩
"Standard Query Operators". Microsoft. Retrieved 2007-11-30. http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/standard_query_operators.doc ↩
"Enumerable Class". msdn. Microsoft. Retrieved 15 February 2014. http://msdn.microsoft.com/en-us/library/system.linq.enumerable(v=vs.110).aspx ↩
Skeet, Jon (23 March 2019). C# in Depth. Manning. ISBN 978-1617294532. 978-1617294532 ↩
"Query Expressions (F#)". Microsoft Docs. Retrieved 2012-12-19. https://docs.microsoft.com/en-gb/dotnet/fsharp/language-reference/query-expressions ↩
"LINQ Framework". Retrieved 2007-11-30. http://msdn.microsoft.com/en-us/library/bb397921.aspx ↩
"Enumerable.ElementAt". Retrieved 2014-05-07. http://msdn.microsoft.com/en-us/library/bb299233.aspx ↩
"Enumerable.Contains". Retrieved 2014-05-07. http://msdn.microsoft.com/en-us/library/bb352880.aspx ↩
"Enumerable.Count". Retrieved 2014-05-07. http://msdn.microsoft.com/en-us/library/bb338038.aspx ↩
".NET Language-Integrated Query for XML Data". 30 April 2007. Retrieved 2007-11-30. http://msdn2.microsoft.com/hi-in/library/bb308960(en-us).aspx ↩
"LINQ to SQL". Archived from the original on 2013-01-25. Retrieved 2007-11-30. https://archive.today/20130125231336/http://www.hookedonlinq.com/LINQtoSQL5MinuteOverview.ashx ↩
"LINQ to SQL: .NET Language-Integrated Query for Relational Data". 30 April 2007. Retrieved 2007-11-30. http://msdn2.microsoft.com/hi-in/library/bb425822.aspx ↩
"LINQ to DataSets". Archived from the original on 2013-01-25. Retrieved 2007-11-30. https://archive.today/20130125171110/http://www.hookedonlinq.com/LINQtoDatasets.ashx ↩
Vider, Guy (2007-12-21). "LINQ Performance Test: My First Visual Studio 2008 Project". Retrieved 2009-02-08. http://www.codeproject.com/KB/dotnet/LINQ_Performance_Test.aspx ↩
Parsons, Jared (2008). "Increase LINQ Query Performance". Microsoft Developer Network. Retrieved 2014-03-19. While it is true that LINQ is powerful and very efficient, large sets of data can still cause unexpected performance problems http://msdn.microsoft.com/en-us/magazine/cc721610.aspx ↩
Alva, Jaime (2010-08-06). "Potential Performance Issues with Compiled LINQ Query Re-Compiles". Microsoft Developer Network. Retrieved 2014-03-19. When calling a query multiple times with Entity Framework the recommended approach is to use compiled LINQ queries. Compiling a query results in a performance hit the first time you use the query but subsequent calls execute much faster http://blogs.msdn.com/b/appfabriccat/archive/2010/08/06/potential-performance-issues-with-compiled-linq-query-re-compiles.aspx? ↩
Kshitij, Pandey (2008-05-25). "Performance comparisons LinQ to SQL, ADO, C#". Retrieved 2009-02-08. http://www.codeproject.com/KB/dotnet/LinQ_Performance_net3_5.aspx ↩
"ParallelEnumerable Class". Retrieved 2014-05-07. http://msdn.microsoft.com/en-us/library/dd413602(v=vs.110).aspx ↩
"Programming in the Age of Concurrency: Concurrent Programming with PFX". Retrieved 2007-10-16. http://channel9.msdn.com/Showpost.aspx?postid=347531 ↩
Eichert, Steve; Wooley, James B.; Marguerie, Fabrice (2008). LINQ in Action. Manning. pp. 56–57 (as reported in the Google Books search link - the book does not have page numbers). ISBN 9781638354628. 9781638354628 ↩
Concepts behind the C# 3.0 language | Articles | TomasP.Net Archived 2007-02-12 at the Wayback Machine http://tomasp.net/articles/csharp3-concepts.aspx ↩
"The Joins Concurrency Library". Retrieved 2007-06-08. http://research.microsoft.com/research/pubs/view.aspx?type=inproceedings&id=2005 ↩