ArticleDirectory
- Delayed execution
- Force immediate execution
Introduction to LINQ
LINQ is short for Language Integrated Query, that is, language integrated query. Why is language integration query? First, the query is based on the data source. In actual operations, there may be many data sources, such as databases, XML, and collections. Integrated Query is to unify the query, you only need the same syntax to query these data sources, instead of writing to different data sources.
The. NET framewor 3.0 + LINQ architecture includes the following.
LINQ is object-oriented, and of course it is also easy to scale. After the emergence of LINQ, a lot of LINQ providers emerged, such as LINQ to JSON, LINQ to Google, and LINQ to iqueryable, through the LINQ architecture combined with some LINQ providers, we can complete basic data source operations, which brings a lot of convenience for development.
LINQ Query
The following operations can be performed in a LINQ query:
- Obtain the data source.
- Create a query.
- Execute the query.
The following is a simple example:
class introtolinq
{< br> static
void main ()
{< br> // the three parts of a LINQ query:
// 1. data source.
int [] numbers = New int [7] {0, 1, 2, 3, 4, 5, 6 };
// 2. query creation.
// numquery is an ienumerable
var numquery =
from num in numbers
where (Num % 2) = 0
select num;
// 3. query execution.
foreach ( int num in numquery)
{< br> console. write ( "{0, 1}" , num);
}< BR >}
The complete query operation is displayed.In LINQ, the query execution (Step 3) is different from the query itself (step 2). In other words, if you only create a query variable, no data is retrieved.
Anonymous local variable
Note thatCodeUsing the VaR keyword tells the compiler to determine the Data Type of the variable. Instead of paying more attention to the type of the variable, we only need to concentrate on writing the LINQ expression.
Data Source
Not all data sources can be queried through LINQ. The data source of LINQ has a basic rule: the LINQ data source is any object that supports the generic ienumerable <t> interface or the interface inherited from this interface.
In the above example, because the data source is an array, the array type supports the ienumerable <t> interface, so we can query it through LINQ.
Note: |
Supports non-generic ienumerable interfaces (such as arraylist) and can also be used as a LINQ data source. For more information, see How to: Query arraylist using LINQ. |
Query
Query is the process of creating a query. In the preceding example, the query expression contains three clauses: from, where, and select. in C #3.0 +, the semantics of these keywords is basically the same as that in SQL, however, the order for writing Query expressions is different here. The query can also specify how to sort, group, and structure the information before it returns. Here, we still need to emphasize that in LINQ, the query variable itself does not perform any operations and does not return any data.
Note: |
You can also use the method syntax to represent a query. For more information, see query syntax and method syntax (LINQ ). |
Query execution
Delayed execution
As mentioned above, the query variable itself only stores the query command. The actual query execution will be delayed until the query variable is cyclically accessed in the foreach statement. This concept is called "delayed execution". The following example demonstrates this concept:
// 3. query execution.
Foreach(IntNumInNumquery)
{
Console. Write ("{0, 1 }", Num );
}
The foreach statement is also used to retrieve the query results. For example, in the previous query, the iteration variable num stores each value in the returned sequence (one value at a time ).
Because the query variable itself never saves the query results, you can execute the query as needed. For example, you can use a separate applicationProgramContinuously update the database. In an application, you can create a query to retrieve the latest data and execute the query repeatedly at a certain interval to retrieve different results each time.
Force immediate execution
The query that executes Aggregate functions on a series of source elements must first access these elements cyclically.Count,Max,AverageAndFirstThis is the query type. Because the query itself must use foreach to return results, these queries do not use an explicit foreach statement during execution. Also note that these types of queries return a single value insteadIenumerableSet. The following query returns the even count of the source array:
VaR evennumquery =
From numInNumbers
Where(Num % 2) = 0
Select num;
IntEvennumcount = evennumquery. Count ();
To forcibly execute any query and cache its results immediately, you can call the tolist <tsource> or toarray <tsource> method.
List <Int> Numquery2 =
(From numInNumbers
Where(Num % 2) = 0
Select num). tolist ();
// Or like this:
// Numquery3 is still an int []
VaR numquery3 =
(From numInNumbers
Where(Num % 2) = 0
Select num). toarray ();