Lambda Expression Tree, lambda expression

Source: Internet
Author: User
Tags xml xpath

Lambda Expression Tree, lambda expression

I will throw some questions first

How can I find the information of a person named zhangsan from a table in the sqlserver database? Simple SQL statement: select * from _ yourTable where name = 'hangsan'. Now the same data is stored in the xml file. What should I do if I find Zhang San? (Not using linq to xml) is not complex, and xml XPath can also be used. What if the data is stored in other types of databases? O (begin □begin) o continue to write SQL statements that may be different ..................... in this way, is it possible to design a Data Structure for storing the same data in different media (memory, xml, remote database) to express this meaning concisely, what is the corresponding processing method for parsing different storage media? You only need to be able to parse the following binary tree data structure. To express this meaning, we need to use the lambda Expression Tree, the main character of today. If lambda is used as a delegate parameter, it will be compiled as an anonymous function at the underlying layer, and if it is passed to C # Expression, it will become an Expression Tree, this is equivalent to the information stored to describe the data structure. This information can be analyzed and then processed. The following Expression <Func <string, bool> filter = name => name = "zhangsan"; filter is different from the delegate object, and it is not executable code, if a filter () Compilation error occurs, what should I do if I want to change the information from an Expression Tree to executable code? As follows:
 Expression< Func<string , bool >> filter = x => x == "zhangsan" ;            var compile = filter.Compile();            bool iszhangsan = compile("zhangsan" );            Console.WriteLine(iszhangsan);            Console.ReadKey();

You can also parse the tree structure, Expression <Func <string, bool> filter = x => x = "zhangsan "; // We can parse this expression tree // from the name, we can also see that the binary tree BinaryExpression body = (BinaryExpression) filter. body; ParameterExpression left = (ParameterExpression) body. left; ConstantExpression right = (ConstantExpression) body. right; string type = body. nodeType. toString (); Console. writeLine ("the left side of the tree is {0}, node symbol {1}, right side of the tree {2}", left, type, right );

The biggest use of the expression tree is not to compile it into an anonymous method for execution, but to store code that I personally think is called "behavior information" that can be executed (such as C # code) it is a lambda statement that complies with certain specifications, if it is an expression tree, it performs lexical analysis and syntax analysis to form a data structure that represents behavior information. (Note that not all lambda expressions can be converted into an Expression Tree.) A brilliant implementation of the above idea is, in particular, let's take a look at such a linq statement var query = from c in db. customers where c. city = "Nantes" select new {c. city, c. companyName}; The IQueryable type is returned by linq to SQL. Let's take a look at the definition of IQueryable and we can see that there is an expression tree. This expression tree stores the behavior information to be expressed by the above linq statement, provider can display the next step to translate this behavior information into a new executable code program, such as linq to sqlserverprovider (built-in) linq to oracle. The reason why C # code is translated into expression tree data information is translated into executable code. In the end, you must generate an SQL statement string and send it to a remote database through the network before querying the result. Of course, how to get the expression tree from the code above and how to analyze the appropriate SQL statement is not covered in the scope of the discussion. There are complicated algorithms here. For the returned result of linq to objects, IEnumerable does not need any expression tree to store information, because it is used to query data in the same memory and directly perform logical search. With the help of a powerful expression tree, linq is booming. For example, the linq to amazon project is to analyze the expression tree and convert it to webservice for various operations. The final conclusion Expression Tree is used to store behavior information and interact with various programs to realize executable code transformation between different languages on different platforms.

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.