C # learning Note (ix): LINQ and Expression trees

Source: Internet
Author: User
Tags joins

Linq

LINQ: Language-integrated queries (Language Integrated query) is a set of extensions for the C # and Visual Basic languages. It allows writing C # or Visual Basic code to manipulate memory data in the same way as querying a database.

with LINQ technology, we can query any form of data using a SQL-like syntax. The data sources that LINQ supports so far are SQL Server, Oracle, XML (an application under the standard Universal Markup Language), and a collection of data in memory. Developers can also add more data sources, such as MySQL, Amazon, or even googledesktop, using the extension framework they provide.
Origin

The designer of. NET defines a series of extension methods in the class library that make it easier for users to manipulate collection objects, and that is, these extension methods form the query operators of LINQ.

Constraints

The extension methods of LINQ are extended for objects that implement the IEnumerable interface, meaning that these extension methods can be used as long as the IEnumerable interface is implemented.

Grammar

LINQ queries have two syntaxes to choose from: Query expression and method syntax (Fluent Syntax).

. The net common language runtime (CLR) does not have the concept of a query expression. Therefore, the compiler converts the query expression to the method syntax, called the extension method, at program compile time. So using method syntax allows us to get closer to and understand the implementation and nature of LINQ, and some queries can only be represented as method invocations. On the other hand, query expressions are usually simpler and easier to read. In any case, the two syntaxes are complementary and compatible, and we can mix query expressions and method syntax in a single query.

Query keywords

Clause

Description

Source

Specifies the data source and range variables (similar to iteration variables).

where

Based on one or more logical "and" and logical "or" operators (&& or | | | | A delimited Boolean expression filters the source element.

Select

Specifies the type and form of the elements in the sequence returned when the query is executed.

Group

Groups the query results by the specified key value.

Into

Provides an identifier that can act as a reference to the result of a join, group, or select clause.

By

The default comparer based on the element type sorts the query results in ascending or descending order.

Join

Joins two data sources based on an equality comparison between two specified match criteria.

Let

Introduces a range variable that stores the results of sub-expressions in a query expression.

Inch

The context keyword in the join clause.

On

The context keyword in the join clause.

equals

The context keyword in the join clause.

By

The context keyword in the group clause.

Ascending

The context keyword in the By clause.

Descending

The context keyword in the By clause.

Syntax examples

Here are two examples of query expression and method syntax (Fluent Syntax), and the effect is to remove even numbers from the array.

1 usingSystem;2 usingSystem.Linq;3 4 namespaceStudy5 {6     class Program7     {8         Private Static voidMain (string[] args)9         {Ten             //defining a data source One             int[] Nums = {0,1,2,3,4,5,6,7,8,9}; A  -             //using query Expressions -             varQUERYRESULT1 = fromNuminchNumswhereNum%2==0 Selectnum; the  -             foreach(intNuminchqueryResult1) -             { -Console.Write ("{0,1}", num); +             } -  + Console.WriteLine (); A  at             //using method Syntax -             varQUERYRESULT2 = Nums. Where (num = num%2==0); -  -             foreach(intNuminchqueryResult2) -             { -Console.Write ("{0,1}", num); in             } -  to Console.read (); +         } -     } the}
manipulating XML Using LINQ

Before c#3.0, the use of System.Xml.XmlDocument to process XML data, the operation is slightly cumbersome, below we look at an example of using LINQ to manipulate XML data:

1 usingSystem;2 usingSystem.Linq;3 usingSystem.Xml.Linq;4 5 namespaceStudy6 {7     class Program8     {9         Private Static stringXMLString =Ten             "<Persons>"+ One             "<person id= ' 1 ' >"+ A             "<Name> Zhang San </Name>"+ -             "<Age>18</Age>"+ -             "</Person>"+ the             "<person id= ' 2 ' >"+ -             "<Name> John Doe </Name>"+ -             "<Age>28</Age>"+ -             "</Person>"+ +             "<person id= ' 3 ' >"+ -             "<Name> Harry </Name>"+ +             "<Age>38</Age>"+ A             "</Person>"+ at             "</Persons>"; -  -         Private Static voidMain (string[] args) -         { -XElement xmldoc =Xelement.parse (xmlstring); -  in             varQueryResult = fromElementinchXmldoc.elements (" Person") -                 whereElement. Element ("Name"). Value = ="John Doe" to                 Selectelement; +  -             foreach(varXElementinchQueryResult) the             { *Console.WriteLine ("Name:"+ Xelement.element ("Name"). Value +", the ID is:"+ Xelement.attribute ("Id"). Value); $             }Panax Notoginseng  - Console.read (); the         } +     } A}
The Nature of LINQ

LINQ, in our view, adds new syntax and features, but in fact there is nothing new for the compiler to add, and the LINQ query statements written are actually translated into lambda expressions and extension methods that the compiler can recognize after compiling.

An expression tree

An expression tree is a data structure that combines lambda expressions into a tree structure, and its main purpose is to construct dynamic queries in LINQ.

Good text link

Expression trees in C #

Build Your Own LINQ Provider (above): Expression Tree Secrets

You can't say it. C # Features-expression tree

Mastering An expression tree

Lambda expressions and expression trees in C #

C # simplifies function calls with lambda expression trees

Elegant calculation of 24 points with C # expression tree

C # learning Note (ix): LINQ and Expression trees

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.