[C #] the basis of the expression tree

In Linq to Objects, the extension method needs to be a delegate type as a parameter, so that you can be given lambda expression parameter. lambda expression may be given Expression <T> type parameter.

c # compiler different behavior types defined according to the lambda expression. If the type is Expression <T>, the compiler creates an expression tree from a lambda expression and stored in the program. In this way, you can analyze the expression tree during operation and optimized in order to query the data source.

Enumerable defines a class is not the only Where extension method () class, Queryable <T> class also defines Where extension method. Enumerable class distinction is a delegate type Fun <T, bool> predicate as a parameter, Queryable <T> class is Expreesion <T> as the type.

Expression Expressions

// delegate type 
Func <Racer, BOOL > = R & lt = the predicateA> r.Country == " Brazil " ;
 // expression tree type of 
the Expression <Func <Racer, BOOL >> R & lt prdeicate = => r.Country == " Brazil "

In addition to the commission, the compiler will put on an expression tree assembly. Expressions can be read during operation.

Expression tree from the class derived from the abstract base class member of Expression. Expression classes and Expression <T> different. Inherited from

Expression class expression class has BinaryExpression, ConstantExpression, InvocationExpression, lambdaExpression, NewExpression, NewArrayExpression, TemaryExpression and Unary Expression and so on. The compiler creates an expression tree from a lambda expression.

For example, lambda expressions r.Country == "Brazil" used ParameterExpression, MemberExpression, ConstantExpression and MethodCallExpression, to create an expression tree. Concentrated and the tree is stored in the program. After the tree is used during operation, create a query to optimize the underlying data source.

  DisplayTree () method to display in the console expression tree graphically. Wherein a passed Expression object. And depending on the type of expression, the expression of some of the information written to the console. Depending on the type of expression, calling DisplayTree () method recursively.

   DisplayTree () method does not handle all types, the type of treatment has ExpressionType.Lambda, ExpressionType.Constant, ExpressionType.Parameter, ExpressionType.Equal, ExpressionType.AndAlso, ExpressionType.GreaterThan, ExpressionType.MemberAccess,

  An example of using Expression <T> is the type of client ADO.NET Entity Framework and WCF data service provider. These technologies define the extension method using Expression <T> parameter, so, Linq provider to access the database can read expressions, create optimized during a run queries to retrieve data from the database.

 

Guess you like

Origin www.cnblogs.com/SignX/p/11509652.html