I have just come into contact with LINQ.
Popular Science:
Language INtegrated Query (LINQ)
Is a Microsoft technology. It adds a natural query SQL syntax to the. NET Framework programming language and supports Visual Basic. NET and C.
LINQ defines about 40 query operators, such as select, from, in, where, and order by (C ).
You can use these operators to write query statements. However, these queries can also be based on many types of data. Each data type requires a separate LINQ type.
Basic Syntax:
- Key words of LINQ: from, select, in, where, group by, orderby ,...
- Note: it must end with select or group.
- Writing Method of LINQ:
1) from temporary variable in implements the IEnumerable <T> interface object
Where condition expression
[Orderby condition]
[Group by condition]
Value queried in the select Temporary Variable
2) the object that implements the IEnumerable interface. The name of the LINQ method (lambda expression ). For example:
String input = "hellow world ";
Int count = input. Count (w => w = 'O'); // query the number of times the letter o appears.
You need to implement the IEnumerable <T> interface for objects that can use LINQ. In addition, the query expression of LINQ is compiled only once when an object is created.
- The full name of LINQ: Language-Integrated Query
- Namespace: System. Linq;
Note: Linq is a technology that appears in. NET Framework 3.5. Therefore, you must select version 3.5 or later when creating a new project. Otherwise, it cannot be used.
After you select the. NET Framework of version 3.5 or later, the created Project automatically contains the namespace of System. Linq.
Next, let's look at the code implementation:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace LINQ {public class Category {public int Age {get; set;} public string Prase {get; set ;}} public class Program {static void Main (string [] args) {// List of operations on the data set <string> names = new List <string> {"Tom ", "Jack", "Jim", "Jackson", "Key", "Kitty"}; var nameJ = from n in names where n. startsWith ("J") orderby n select n; foreach (var name in nameJ) {Console. writeLine (name);} Console. writeLine ("-------------------------"); names. add ("Jack1"); names. add ("Jack2"); names. add ("Jack3"); foreach (string name in nameJ) {Console. writeLine (name);} Console. writeLine ("-------------------------"); List <Category> cs = new List <Category> () {new Category {Age = 22, Prase = "phase 13 "}, new Category {Age = 22, Prase = "12"}, new Category {Age = 22, Prase = "14"}, new Category {Age = 23, prase = "14"}, new Category {Age = 20, Prase = "13"}, new Category {Age = 24, Prase = "14 "}, new Category {Age = 25, Prase = "14"}, new Category {Age = 23, Prase = "13"}, new Category {Age = 25, prase = "14 period" }}; var s = (from s1 in cs where s1.Age> 21 & s1.Prase = "14 period" orderby s1.Age descending select s1 ). take (3); foreach (var c1 in s) {Console. writeLine (c1.Age + "" + c1.Prase);} Console. writeLine ("-------------------------"); var ss = from s1 in cs group s1 by s1.Prase into c orderby c. count () descending, c. key where c. count ()> = 2 select c. key + c. count (); foreach (var s1 in ss) {Console. writeLine (s1);} Console. writeLine ("-------------------------"); object [] data = {"hello", 33, "what", 36, "fine", 39, "thanks "}; var str = data. ofType <string> (); foreach (var a in str) {Console. writeLine (a);} Console. readKey ();}}}
The above Code except the LINQ query syntax is the syntax we are familiar with, while the LINQ query syntax is very similar to the SQL query syntax, except for the order.
Q: Why does the syntax of a linq query start with the from keyword instead of the select keyword? The statement at the beginning of select is more similar to the SQL statement and easier to understand?
A: Simply put, the select keyword is placed behind the intelligent perception (Intelisence) function of the IDE.
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 group.
The Group by in LINQ should not be confused with the Group by in SQL. Because SQL is a two-dimensional structure, some logic of Group by is restricted by the two-dimensional structure and cannot be as flexible as Group by in LINQ.
In fact, the query syntax of LINQ has the following two forms:
Query Method: (Methord Syntax)
The Extension Method and Lambda expression method defined in the System. Linq. Enumerable class are used for query.
Reference:Lambda expressions (C # programming guide): https://msdn.microsoft.com/zh-cn/library/bb397687.aspx
Query statement method: (Query Syntax) a Query method that is closer to the SQL Syntax and has better readability.
Advantages:
1. Get started without complex learning processes
2. Write less code to create a complete application.
3. faster development of applications with fewer errors.
4. data sources can be merged without any strange programming skills.
5. Higher development efficiency for new developers.
6. Any object or data source can be customized to implement the Linq adapter, which makes data interaction really convenient.
Function Support:
The following Common Language Runtime (CLR) methods and attributes are supported, because they can be converted in the query expression 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 Microsoft. VisualBasic. DateAndTime equivalent in Visual Basic and the DatePart method.
Math Member |
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 Member |
Supported OData Functions |
TypeIs |
Bool, isof (type, p0) |
The client may also be able to compute other CLR functions on the client. Any expressions that cannot be calculated on the client and cannot be converted to valid request Uris for calculation on the server will causeNotSupportedException.
Reference:Write the query (LINQ) in C #: https://msdn.microsoft.com/zh-cn/library/bb397900 (v = VS.90). aspx? Lc = 1, 2052