Introduction to the two query syntaxes of the linq to Objects series (2)

Source: Internet
Author: User

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

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.