Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Language Integrated Query (LINQ)

  • 2023-12-15
Feedback

In this article

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have to learn a different query language for each type of data source: SQL databases, XML documents, various Web services, and so on. With LINQ, a query is a first-class language construct, just like classes, methods, and events.

When you write queries, the most visible "language-integrated" part of LINQ is the query expression. Query expressions are written in a declarativequery syntax. By using query syntax, you perform filtering, ordering, and grouping operations on data sources with a minimum of code. You use the same query expression patterns to query and transform data from any type of data source.

The following example shows a complete query operation. The complete operation includes creating a data source, defining the query expression, and executing the query in aforeach statement.

// Specify the data source.int[] scores = [97, 92, 81, 60];// Define the query expression.IEnumerable<int> scoreQuery =    from score in scores    where score > 80    select score;// Execute the query.foreach (var i in scoreQuery){    Console.Write(i + " ");}// Output: 97 92 81

You might need to add ausing directive,using System.Linq;, for the preceding example to compile. The most recent versions of .NET make use ofimplicit usings to add this directive as aglobal using. Older versions require you to add it in your source.

Query expression overview

  • Query expressions query and transform data from any LINQ-enabled data source. For example, a single query can retrieve data from an SQL database and produce an XML stream as output.
  • Query expressions use many familiar C# language constructs, which make them easy to read.
  • The variables in a query expression are all strongly typed.
  • A query isn't executed until you iterate over the query variable, for example in aforeach statement.
  • At compile time, query expressions are converted to standard query operator method calls according to the rules defined in the C# specification. Any query that can be expressed by using query syntax can also be expressed by using method syntax. In some cases, query syntax is more readable and concise. In others, method syntax is more readable. There's no semantic or performance difference between the two different forms. For more information, seeC# language specification andStandard query operators overview.
  • Some query operations, such asCount orMax, have no equivalent query expression clause and must therefore be expressed as a method call. Method syntax can be combined with query syntax in various ways.
  • Query expressions can be compiled to expression trees or to delegates, depending on the type that the query is applied to.IEnumerable<T> queries are compiled to delegates.IQueryable andIQueryable<T> queries are compiled to expression trees. For more information, seeExpression trees.

How to enable LINQ querying of your data source

In-memory data

There are two ways you enable LINQ querying of in-memory data. If the data is of a type that implementsIEnumerable<T>, you query the data by using LINQ to Objects. If it doesn't make sense to enable enumeration by implementing theIEnumerable<T> interface, you define LINQ standard query operator methods, either in that type or asextension methods for that type. Custom implementations of the standard query operators should use deferred execution to return the results.

Remote data

The best option for enabling LINQ querying of a remote data source is to implement theIQueryable<T> interface.

IQueryable LINQ providers

LINQ providers that implementIQueryable<T> can vary widely in their complexity.

A less complexIQueryable provider might access a single method from a Web service. This type of provider is very specific because it expects specific information in the queries that it handles. It has a closed type system, perhaps exposing a single result type. Most of the execution of the query occurs locally, for example by using theEnumerable implementations of the standard query operators. A less complex provider might examine only one method call expression in the expression tree that represents the query, and let the remaining logic of the query be handled elsewhere.

AnIQueryable provider of medium complexity might target a data source that has a partially expressive query language. If it targets a Web service, it might access more than one method of the Web service and select which method to call based on the information that the query seeks. A provider of medium complexity would have a richer type system than a simple provider, but it would still be a fixed type system. For example, the provider might expose types that have one-to-many relationships that can be traversed, but it wouldn't provide mapping technology for user-defined types.

A complexIQueryable provider, such as theEntity Framework Core provider, might translate complete LINQ queries to an expressive query language, such as SQL. A complex provider is more general because it can handle a wider variety of questions in the query. It also has an open type system and therefore must contain extensive infrastructure to map user-defined types. Developing a complex provider requires a significant amount of effort.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo