It has been released for several years and now you can learn about it. I want to get familiar with this idea and lay the foundation for learning EF or other ORM frameworks in the future.
1. Basic Introduction:
Language Integrated Query (LINQ, Language Integrated Query)It is an innovative feature introduced in Visual Studio 2008 and. NET Framework 3.5. It builds a bridge between the object field and the data field. So far, LINQ is still supported.
So what is LINQ? Why should we introduce this technology?
in development, we will involve different data sources, such as XML documents, SQL databases, and ADO. net dataset, data in any object set that supports the ienumerable or generic ienumerable interface, and any Programs available data in other formats, to obtain data from different data sources, we may need to learn different operations. Therefore, by providing a consistent model for using data across various data sources and formats, LINQ simplifies this situation, we You can use the same basic encoding mode (that is, the LINQ syntax) to query and operate data.
The core of LINQ is the query expression.A query is an expression used to retrieve data from a data source. Queries are usually expressed in a special query language.
In. NET Framework 3.5, LINQ has five functions:
(1) LINQ to objects (object)
(2) LINQ to dataset (for datasets)
(3) LINQ to SQL (for SQL databases)
(4) LINQ to entities (object)
(5) LINQ to XML (for XML)
Let's take a look at the overall LINQ architecture:
2. query by using LINQ
All LINQ query operations are composed of the following three different operations:Get Data Source,Create query,Execute Query.
Let's take an example for comparison: the query array is larger than 2 and sorted in ascending order.
Static Void Main ( String [] ARGs ){ Int [] Numbers = New Int [] { 1 , 5 , 3 , 6 , 2 }; List < Int > List = New List <Int > {}; Foreach ( Int Num In Numbers ){ // Number greater than 2 If (Num> 2 ) {List. Add (Num );}} // List sorting List. Sort (); console. Write ( " The array is larger than 2 and sorted in ascending order: " ); Foreach ( Int Ordernum In List) {console. Write ( " {0} " , Ordernum);} console. readkey ();}
Does it seem complicated? using LINQ can simplify the operation:
Static Void Main ( String [] ARGs ){ // Data Source Int [] Numbers = New Int [] { 1 , 5 , 3 , 6 , 2 }; // Create Query VaR Query = From Num In Numbers Where Num> 2 Orderby Num ascending Select Num; console. Write ( " The array is larger than 2 and sorted in ascending order: " ); // Execute Query Foreach ( Int Num In Query) {console. Write ( " {0} " , Num);} console. readkey ();}
The complete query operation is displayed. In LINQ, data is not queried when we create a query expression. Data is retrieved only when the query is executed. In other words, if you only create a query variable, no data is retrieved.
When the program is broken, it will find that foreach will jump to the query expression and then retrieve the data, which is delayed execution.
Specific understanding:
Data source:The LINQ data source supports generics.Ienumerable<T>Interface or any object of the interface inherited from this interface.
Create query:The query is stored in the query variable and initialized with the query expression. SpecificallyLINQ query syntax and method syntax (C #).
execution query: can be divided into delayed execution and forced immediate execution.
(1) delayed execution:The query variable itself only stores the query command. The actual query execution will be delayedForeachStatement.Example.
(2) Force Execution immediately: The query that executes Aggregate functions on a series of source elements must first access these elements cyclically. Count, Max, average, and first belong to this type of query. Because the query itself must useForeachTherefore, these queries are not explicitly executed.ForeachStatement. Note that these types of queries return a single value instead of the ienumerable set. Example: count the number larger than 2 in the array.
Int [] Numbers = New Int [] {1 , 5 , 3 , 6 , 2 }; Int Query = ( From Num In Numbers Where Num> 2 Select Num). Count (); console. Write ( " Number of arrays larger than 2: {0} " , Query); console. readkey (); // Returns the number greater than 2.
Author: forevernome
Source: http://www.cnblogs.com/ForEvErNoME/
You are welcome to repost or share it, but be sure to declare it Article Source. If the article is helpful to you, I hope you can Recommendation Or Follow