LINQ: start to use the query syntax and method syntax in LINQ (5)-LINQ.

Source: Internet
Author: User

LINQ: start to use the query syntax and method syntax in LINQ (5)-LINQ.
Getting started with the query syntax and method syntax in LINQ (5 )-

In the Expression Language Integrated Query (LINQ), the use of the LINQ query syntax, most of the queries in the document are written. However, when compiling code, you must convert the query syntax into a method, which requires the. NET public Language Runtime (CLR ). The names of the standard query operators used to call these methods are similar.Where,Select,GroupBy,Join,MaxAndAverage. You can call these methods to directly use the method syntax instead of the query syntax.

The query syntax and method syntax have the same semantics. However, many people find that the query syntax is simpler and easier to read. Some queries must be represented as method calls. For example, you must use a method call to query the number of elements and specified conditions. You must also use the method to retrieve the maximum value of the element in the source sequence query. The reference documentation for standard query operators in the System. Linq namespace usually uses method syntax.

 

I. standard query operator extension method the following example demonstrates a simple query expression and a semantic equivalent query written as a method-based query.
 1         static void Main(string[] args) 2         { 3             var nums = new int[4] { 1, 2, 3, 4 }; 4  5             var qureyNums = from n in nums 6                             where n % 2 == 0 7                             orderby n descending 8                             select n; 9 10             Console.WriteLine("qureyNums:");11             foreach (var n in qureyNums)12             {13                 Console.WriteLine(n);14             }15 16             var queryNums2 = nums.Where(n => n % 2 == 0).OrderByDescending(n => n);17             Console.WriteLine("qureyNums2:");18             foreach (var n in queryNums2)19             {20                 Console.WriteLine(n);21             }22 23             Console.Read();24         }    

 

The outputs of the two examples are the same. You can see that the two types of query variables are the same: IEnumerable <T>.

To learn about method-based queries, let's further analyze them. Note: On the right side of the expression, the where clause is now represented as an instance method for the numbers object. When you call this object again, its type is IEnumerable <int>. If you are familiar with the generic IEnumerable <T> interface, you will understand that it does not have WhereMethod. However, if you call the intelliisense Completion list in Visual Studio IDE, you will not only see WhereAnd you will see many other methods, such Select, Selectworkflow, JoinAnd Orderby. The following are all standard query operators.

Although it seems that IEnumerable <T> has been redefined to include these additional methods, this is not actually the case. These standard query operators are implemented as "extension methods.

 

II. In the previous example, the Lambda expression notifies the condition expression (num % 2 = 0) as an inline parameter. WhereMethod: Where (num => num % 2 = 0). This inline expression is called a lambda expression. Writing code as an anonymous method, generic delegate, or Expression Tree is a convenient method. Otherwise, writing is much more complicated. In C, =>It is a lambda operator and can be read as "goes ". The num on the left side of the operator is the input variable, which corresponds to the num in the query expression. The compiler can infer the num type because it understands that numbers is a generic IEnumerable <T> type. Lambda expressions are the same as expressions in the query syntax or any other C # expressions or statements. They can include method calls and other complex logic. The "return value" is the expression result. Iii. query combination in the above code example, please note OrderByThe method is WhereIs called using the vertex operator. WhereGenerate the filter sequence, and then OrderbySort the sequence to operate it. Because the query will return IEnumerableSo you can combine these queries in the method syntax by linking method calls. This is the operations performed by the compiler in the background when you write a query using the query syntax. Because the query variable does not store the query result, you can modify it at any time or use it as the basis for a new query, even after executing it. [Source] Text and some images in this article are mainly taken from Microsoft official documents.

 

Related Article

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.