Expression Tree in LINQ

Source: Internet
Author: User

In the previous article, C # cainiao introduced "C #'s delegate/event/Action/func/predicate keyword" and "Analysis of Dynamic, VAR, and object keywords in C #4.0". today, let's continue to talk about LINQ expressions.

After. net3.0, you can use a LINQ expression, that isFrom a in set where a. x = y selectThe essence of such expressions is the compiler's support for specific keywords provided by iqueryable objects.

For details, see system. LINQ. Expressions namespace and building LINQ queries at runtime in C #.

In LINQ, the expression tree is used to represent structured queries against data sources. These data sources implement iqueryable (of T ). For exampleProgramImplements the iqueryable (of T) interface for querying relational data storage. The C # and Visual Basic compilers compile queries for such data sourcesCodeThis code will generate an expression tree during runtime. Then, the query provider can traverse the expression tree data structure and convert it into a query language suitable for the data source. The expression tree can also be used in LINQ to represent a Lambda expression assigned to a variable of the expression type (of tdelegate. The expression tree can also be used to create dynamic LINQ queries. For more information, see How to: use the expression tree to generate dynamic queries. You can also use the expression tree to generate a LINQ provider.

In LINQ, we use a LINQ query expression to obtain data from the database. It is shown that the database does not know the LINQ syntax. It only knows SQL statements, and the expression tree will be used in this case, it parses the LINQ query expression into a data structure, analyzes the data structure, and then combines it into the SQL statements required by the SQL database.

Example 1:

1:UsingSystem. LINQ. expressions;
 
2: 
 
3:Expression <func <student,Bool> Expression = r => r. Graduate = 1 & R. Name ="Jacky";
 
4:Func <student,Bool> D = expression. Compile ();
 
5:BoolResult = D (NewStudent {name ="Tummy", Graduate = 2 });
 
6:Console. writeline (result +"-- This is LINQ expression result");

Example 2:

 
1:Static VoidRaisesomepropchanged <t> (expression <func <t> propertyexpression)
 
2:{
3:Func <t> d = propertyexpression. Compile ();
 
4:T result = D ();
 
5:Console. writeline (result );
 
6:}
 
7: 
 
8:Raisesomepropchanged () =>"ABC");

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.