This is a bit similar to SQL statements.
Select Syntax:
There are three basic actions of all the LINQ queries:
1. contains data sources
2. Create a query
3. Execute the query
The following is a piece of code, which indicates three parts of the query operation.
Using system;
Using system. Collections. Generic;
Using system. LINQ; // This Is The namespace to which LINQ belongs. It is unavailable in vs2003 and vs2005, and is now available!
Using system. text;
Namespace consoleapplication2
{
Class Program
{
Static void main (string [] ARGs)
{
// Step 2: Include the data source.
String [] strings = new string [4] {"Wuxi City, Jiangsu Province", "Jiangnan University-Jiangnan No. 1 Emy", "taoxi Emy-spam", "mingyan Xiangyu "};
// Step 2: Create a query.
VaR query = from item in strings
// Note: the keyword var here is only available in C #3.0. It is called "implicit type". It will tell the compiler to determine the type of the variable by yourself, the compiler determines different types of variables based on different environments, and users of the language can be lazy, ^_^. For example:
VaR A = 5; the compiler determines that the type of variable A is int.
VaR STR = "Taihu College, Jiangnan University"; the compiler determines that the STR variable type is string.
Another point is that VaR can only be used for local variables!
Orderby item
Group item by item. length into lengthgroups
Orderby lengthgroups. Key descending
Select lengthgroups;
// The above code is a LINQ query statement, which is very concise and can be understood as an example. The syntax structure is similar to that of an SQL statement, the reason for "similarity" is that it is a little different from the SQL statement. The "select" keyword of the SQL statement is at the beginning, while the LINQ keyword is at the end.
// Step 4: Execute the query.
Foreach (VAR item in query)
{
Console. writeline ("strings of length", item. Key );
Foreach (var val in item)
{
Console. writeline (VAL );
}
}
}
}
}
In the preceding example, the data source type is array, which serves as the data source of the LINQ query. This is how the LINQ query is executed. The into keyword indicates that the result of the previous query is considered as the generator of the subsequent query, Which is used together with groupby.