C #3.5 new features (2) -- LINQ

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.