Popular Science:
Language integration Query (Language INtegrated query,linq)
is a Microsoft technology that adds a natural query of SQL syntax to the. NET Framework programming language, which supports visual Basic. NET and the C # language.
LINQ defines about 40 query operators, such as Select, from, in, where, and order by (C #).
Use these operators to write query statements. However, these queries can also be based on many types of data, and each data type requires a separate LINQ type.
Basic syntax:
- LINQ keywords: From, select, in, where, group by, ...
- LINQ Note: Must end with select or group by.
- Syntax for LINQ:
1) from temp variable in implement Ienumerable<t> interface object
Where Condition expression
[Conditions]
[GROUP BY conditions]
The value queried in the Select Temp variable
2) The object that implements the IEnumerable interface. Linq method Name (lambda expression). Such as:
string input = "Hellow world";
int count = input. Count (w=>w = = ' O '); Query the number of occurrences of the letter O
objects that can use LINQ need to implement the Ienumerable<t> interface. and LINQ query expressions are compiled at the time the object is created.
- Full name of LINQ: Language-integrated Query
- namespaces: System.Linq;
Note: LINQ is a technology that appears in the. NET Framework 3.5, so you must choose 3.5 or higher when creating a new project, otherwise you will not be able to use it.
After you select the. NET framework of 3.5 or later, the System.Linq namespace is automatically included in the new project that you create.
Then look at the code implementation:
View Code
The above code, in addition to the LINQ query syntax, is the syntax we are familiar with, and the LINQ query syntax is similar to SQL query syntax, except in order of precedence.
Q: Why does the LINQ query syntax start with the FROM keyword, rather than the SELECT keyword? Select at the beginning of this kind of writing and SQL is more similar to the wording, more understandable?
A: Simply put, for the IDE's IntelliSense (intelisence) feature, the SELECT keyword is behind.
The INTO keyword indicates that the result of the previous query is considered a generator for subsequent queries, which is used with group by.
Group BY in LINQ is not confused with group by in SQL because it is a two-dimensional structure, and some of the logic of group by is constrained by a two-dimensional structure that cannot be as flexible as group by in LINQ.
In fact, LINQ has the following two forms of query syntax:
Query method: (Methord Syntax)
The main use of the extension method defined in the System.Linq.Enumerable class and the lambda expression way to query
Reference Documentation: Lambda expression (C # Programming Guide): https://msdn.microsoft.com/zh-cn/library/bb397687.aspx
Query statement mode: (query Syntax) A query method that is closer to the SQL syntax, which is more readable.
Advantages of Use:
1. No complicated learning process can be started
2. Write less code to create a complete app.
3. Develop applications with fewer errors faster.
4. You can merge data sources without resorting to strange programming techniques.
5, let the new developer more efficient development.
6, any object or data source can be customized to implement the LINQ adapter, for data interaction to bring real convenience.
Function Support:
The following common language runtime (CLR) methods and properties are supported because they can be converted in query expressions to be included in the request URI of the OData service:
String Member |
supported OData functions |
Concat |
String,concat (STRING,P0,STRING,P1) |
Contains |
Bool,substringof (STRING,P0,STRING,P1) |
EndsWith |
Bool,endswith (STRING,P0,STRING,P1) |
IndexOf |
Int,indexof (STRING,P0,STRING,P1) |
Length |
Int,length (STRING,P0) |
Replace |
String,replace (String,p0,string,find,string,replace) |
Substring |
String,substring (String,p0,int,pos) |
Substring |
String,substring (String,p0,int,pos,int,length) |
ToLower |
String,tolower (STRING,P0) |
ToUpper |
String,toupper (STRING,P0) |
Trim |
String,trim (STRING,P0) |
DateTime member 1 |
supported OData functions |
Day |
Int,day (DATETIME,P0) |
Hour |
Int,hour (DATETIME,P0) |
Minute |
Int,minute (DATETIME,P0) |
Month |
Int,month (DATETIME,P0) |
Second |
Int,second (DATETIME,P0) |
Year |
Int,year (DATETIME,P0) |
1 also supports the date and time attributes of the equivalent Microsoft.VisualBasic.DateAndTime in Visual Basic and the DatePart method.
Math Members |
supported OData functions |
Ceiling |
Decimal,ceiling (DECIMAL,P0) |
Ceiling |
Double,ceiling (DOUBLE,P0) |
Floor |
Decimal,floor (DECIMAL,P0) |
Floor |
Double,floor (DOUBLE,P0) |
Round |
Decimal,round (DECIMAL,P0) |
Round |
Double,round (DOUBLE,P0) |
Expression Members |
supported OData functions |
Typeis |
Bool,isof (TYPE,P0) |
The client may also be able to calculate additional CLR functions on the client. NotSupportedExceptionis raised for any expression that cannot be evaluated on the client and cannot be converted to a valid request URI for evaluation on the server.
Reference Document: writing a query in C # (LINQ): https://msdn.microsoft.com/zh-cn/library/bb397900 (v=vs.90). aspx?lc=2052
Just contacted by LINQ