LINQ provides two query syntaxes: query expression and query method syntax. This article is summarized in the following aspects.
1. A simple example containing two query syntaxes
2. query the expression Structure
3. query method-related operators
A simple example containing two query syntaxes
You can use either a query expression or a query method for a LINQ query. You can also combine the two types of queries. The following is a simple example. The Code is as follows.
Namespace LINQDemo3 {class Program {static void Main (string [] args) {int [] numbers = {2, 5, 28, 31, 17, 16, 42 }; // query expression var query = from number in numbers where number <20 select number; // query method (lambda expression) var numsMethod = numbers. where (p => p <20); // two methods of combination var numsCount = (from number in numbers where number <20 select number ). count (); foreach (var item in query) {Console. writeLine ("{0}", item);} foreach (var item in numsMethod) {Console. writeLine ("{0}", item);} Console. writeLine (numsCount); Console. readKey ();}}}
Query expression Structure
The query expression usually starts with "from" and ends with "select", which is not in the same order as the SQL statement, C # One of the reasons for doing so is to allow VS smart sensing to give us more options when we input code. The common structure of a query expression is as follows.
Note: This figure references the image in the http://www.cnblogs.com/heyuquan/p/linq-to-objects.htmldocument of 'ticking rain.
Query Method-related operators
The query method can be divided into filtering, sorting, grouping, statistics, conversion, and other operations. The following figure is a summary.
Note: This figure references the article from 'ticking rain', http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html