Language Integrated Query: Difference between revisions

Content deleted Content added
Monkbot (talk | contribs)
m Task 18 (cosmetic): eval 19 templates: del empty params (5×); del |url-status= (1×);
Monkbot (talk | contribs)
m Task 18 (cosmetic): eval 19 templates: hyphenate params (18×);
Line 22:
'''Language Integrated Query''' ('''LINQ''', pronounced "link") is a [[Microsoft]] [[.NET Framework]] component that adds native data [[Query language|querying]] capabilities to [[List of CLI languages|.NET languages]], originally released as a major part of [[.NET Framework 3.5]] in 2007.
 
LINQ extends the language by the addition of query [[Expression (computer science)|expressions]], which are akin to [[SQL]] statements, and can be used to conveniently extract and process data from [[Array data structure|arrays]], enumerable [[class (computer science)|class]]es, [[XML]] documents, [[relational database]]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<ref name="reactive">{{cite web | url = http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx | title = Rx framework}}</ref> or [[Monad (functional programming)|monadic]] [[Parsing|parsers]].<ref name="parscomb">{{cite web | url = http://blogs.msdn.com/lukeh/archive/2007/08/19/monadic-parser-combinators-using-c-3-0.aspx | title = Monadic Parser Combinators using C#3 | accessdateaccess-date = 2009-11-21}}</ref> 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 interface|fluent-style]] (called method syntax by Microsoft) with these method names, [[Anonymous function#C.23 lambda expressions|lambda expressions]] and [[anonymous type]]s. Many of the concepts that LINQ introduced were originally tested in Microsoft's [[Cω]] research project.
 
Ports of LINQ exist for [[PHP]] ([https://phplinq.codeplex.com/ PHPLinq]), [[JavaScript]] ([https://github.com/mihaifm/linq linq.js]), [[TypeScript]] ([https://github.com/kutyel/linq.ts linq.ts]), and [[ActionScript]] ([http://actionlinq.riaforge.org/ 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).{{citation needed|date=October 2018}}
Line 31:
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 [[Operator (programming)|operator]]s defined by LINQ is exposed to the user as the Standard Query Operator (SQO) [[API]]. The query operators supported by the API are:<ref name="sqo">{{cite web | url = http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/standard_query_operators.doc | title = Standard Query Operators | publisher = Microsoft | accessdateaccess-date = 2007-11-30}}</ref>
;Select:
{{Further|Map (higher-order function)}}
Line 68:
 
The Standard Query Operator API also specifies certain operators that convert a collection into another type:<ref name="sqo"/>
* AsEnumerable: Statically types the collection as an <code>IEnumerable<T></code>.<ref name="Enumerable Class">{{cite web|title=Enumerable Class|url=http://msdn.microsoft.com/en-us/library/system.linq.enumerable(v=vs.110).aspx|work=msdn|publisher=Microsoft|accessdateaccess-date=15 February 2014}}</ref>
* AsQueryable: Statically types the collection as an <code>IQueryable<T></code>.
* ToArray: Creates an array <code>T[]</code> from the collection.
Line 78:
 
===Language extensions===
While LINQ is primarily implemented as a [[library (computing)|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 Sharp (programming language)|C#]] 3.0, [[VB.NET|VB 9.0]], [[F Sharp (programming language)|F#]]<ref name="linq">{{cite web|title=Query Expressions (F#)|url=https://docs.microsoft.com/en-gb/dotnet/fsharp/language-reference/query-expressions|website=Microsoft Docs|accessdateaccess-date=2012-12-19}}</ref> and [[Oxygene (programming language)|Oxygene]], with other languages like [[Nemerle]] having announced preliminary support. The language extensions include:<ref name="linq1">{{cite web | url = http://msdn.microsoft.com/en-us/library/bb397921.aspx | title = LINQ Framework | accessdateaccess-date = 2007-11-30}}</ref>
*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 <code>var</code> keyword. In VB9.0, the <code>Dim</code> keyword without type declaration accomplishes the same. Such objects are still [[strong typing|strongly typed]]; for these objects the compiler infers the types of variables via [[type inference]], which allows the results of the queries to be specified and defined without declaring the type of the intermediate variables.
Line 117:
 
====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 <code>Sequence</code> pattern and allows <code>IEnumerable<T></code> 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.<ref name="Enumerable.ElementAt">{{cite web | url = http://msdn.microsoft.com/en-us/library/bb299233.aspx | title = Enumerable.ElementAt | accessdateaccess-date = 2014-05-07}}</ref><ref name="Enumerable.Contains">{{cite web | url = http://msdn.microsoft.com/en-us/library/bb352880.aspx | title = Enumerable.Contains | accessdateaccess-date = 2014-05-07}}</ref><ref name="Enumerable.Count">{{cite web | url = http://msdn.microsoft.com/en-us/library/bb338038.aspx | title = Enumerable.Count| accessdateaccess-date = 2014-05-07}}</ref>
 
====LINQ to XML (formerly called XLINQ)====
The LINQ to XML provider converts an XML document to a collection of <code>XElement</code> 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.<ref>{{cite web | url = http://msdn2.microsoft.com/hi-in/library/bb308960(en-us).aspx | title = .NET Language-Integrated Query for XML Data | accessdateaccess-date = 2007-11-30}}</ref>
 
====LINQ to SQL (formerly called DLINQ)====
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.<ref>{{cite web | url = http://www.hookedonlinq.com/LINQtoSQL5MinuteOverview.ashx | title = LINQ to SQL | accessdateaccess-date = 2007-11-30 | archive-url = https://archive.is/20130125231336/http://www.hookedonlinq.com/LINQtoSQL5MinuteOverview.ashx | archive-date = 2013-01-25 | url-status = dead }}</ref> However, since SQL Server stores the data as [[relational database|relational data]] and LINQ works with data encapsulated in objects, the two representations must be [[Object-Relational mapping|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.<ref name="ltos">{{cite web | url = http://msdn2.microsoft.com/hi-in/library/bb425822.aspx | title = LINQ to SQL: .NET Language-Integrated Query for Relational Data | accessdateaccess-date = 2007-11-30}}</ref> The correspondence, along with other [[relational model]] attributes such as [[primary key]]s, are specified using LINQ to SQL-defined [[attribute (computing)|attributes]]. For example,
 
<syntaxhighlight lang="csharp">
Line 142:
 
====LINQ to DataSets====
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.<ref>{{cite web | url = http://www.hookedonlinq.com/LINQtoDatasets.ashx | title = LINQ to DataSets | accessdateaccess-date = 2007-11-30 | archive-url = https://archive.is/20130125171110/http://www.hookedonlinq.com/LINQtoDatasets.ashx | archive-date = 2013-01-25 | url-status = dead }}</ref>
 
==Performance==
Line 150:
| date=2007-12-21
|last=Vider|first=Guy
| accessdateaccess-date=2009-02-08}}</ref><ref>{{cite web
| url=http://msdn.microsoft.com/en-us/magazine/cc721610.aspx
| title=Increase LINQ Query Performance
Line 157:
| quote=''While it is true that LINQ is powerful and very efficient, large sets of data can still cause unexpected performance problems''
| date=2008
| accessdateaccess-date=2014-03-19}}</ref>
 
'''LINQ to XML''' and '''LINQ to SQL''' performance compared to ADO.NET depends on the use case.<ref>{{cite web
Line 166:
| quote=''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''
| date=2010-08-06
| accessdateaccess-date=2014-03-19}}</ref><ref>{{cite web
| url=http://www.codeproject.com/KB/dotnet/LinQ_Performance_net3_5.aspx
| title=Performance comparisons LinQ to SQL, ADO, C#
| date=2008-05-25
| last=Kshitij|first=Pandey
| accessdateaccess-date=2009-02-08}}</ref>
 
==PLINQ==
Version 4 of the .NET framework includes [[Parallel Extensions|PLINQ]], or ''Parallel LINQ'', a [[parallel computing|parallel]] execution engine for LINQ queries. It defines the <code>ParallelQuery<T></code> class. Any implementation of the <code>IEnumerable<T></code> interface can take advantage of the PLINQ engine by calling the <code>AsParallel<T>(this IEnumerable<T>)</code> extension method defined by the ParallelEnumerable class in the System.Linq namespace of the .NET framework.<ref name="ParallelEnumerable">{{cite web | url = http://msdn.microsoft.com/en-us/library/dd413602(v=vs.110).aspx | title = ParallelEnumerable Class | accessdateaccess-date = 2014-05-07}}</ref> The PLINQ engine can execute parts of a query concurrently on multiple threads, providing faster results.<ref name="channel9">{{cite web | url = http://channel9.msdn.com/Showpost.aspx?postid=347531 | title = Programming in the Age of Concurrency: Concurrent Programming with PFX | accessdateaccess-date = 2007-10-16}}</ref>
 
==See also==