Documente online.
Zona de administrare documente. Fisierele tale
Am uitat parola x Creaza cont nou
 HomeExploreaza
upload
Upload




Using Lambda Expressions to Create Expression Trees

visual c en


Using Lambda Expressions to Create Expression Trees

In addition to treating data as objects, LINQ provides the ability to treat expressions as data at runtime. LINQ defines a new type, Expression<T>, which represents an expression tree, an in-memory representation of a lambda expression. Expressio 747d32h n trees allow developers to treat lambda expressions as data, allowing for inspection and modification of lambda expressions in code. This feature will be used to enable an ecosystem of third-party libraries that leverage the base query abstractions that are part of LINQ. For example, a database access implementation might leverage this facility to translate expression trees into suitable query statements for a particular kind of database.



Task 1 - Creating an Expression Tree using a Lambda Expression

In this task, you declare an expression tree and print a representation of the expression body.

Add the following using directive to gain access to the types in System.Expressions (at the top of the file):

using System.Linq.Expressions;

Replace the code in the body of the Main method with the following code:

static void Main(string[] args)

)", lt.NodeType,
mult.NodeType, en.Name, three.Value, five.Value);

This code creates an expression tree representation from the lambda expression. It then initializes local variables for each node of the tree. Finally, it outputs a LISP-style representation of the expression, to demonstrate a translation of the lambda expression to the expression tree.

Press Ctrl+F5 to run the code and see the following output in the console window:

(LessThan (Multiply n 3) 5)

Press any key to terminate the application.

Task 2 - Relationship between functions and expressions

In this task, you learn how to convert an expression tree into a delegate that can be executed.

Replace the code in the body of the Main method with the following code:

static void Main(string[] args)

" addOne(5));

Press Ctrl+F5 to run the code and see the result printed to the screen.    Then press any key to terminate the application.

Add these lines below those:

static void Main(string[] args)

", addOne(5));

Expression<Func<int, int>> addOneExpression = n => n + 1;

var addOneFunc = addOneExpression.Compile();

Console.WriteLine("Result: " addOneFunc(5));

Run the program again and notice the same result is printed twice.

It is very easy to create and build expressions that can be compiled dynamically and create new Func objects that pass around code as objects.


Document Info


Accesari: 1044
Apreciat: hand-up

Comenteaza documentul:

Nu esti inregistrat
Trebuie sa fii utilizator inregistrat pentru a putea comenta


Creaza cont nou

A fost util?

Daca documentul a fost util si crezi ca merita
sa adaugi un link catre el la tine in site


in pagina web a site-ului tau.




eCoduri.com - coduri postale, contabile, CAEN sau bancare

Politica de confidentialitate | Termenii si conditii de utilizare




Copyright © Contact (SCRIGROUP Int. 2025 )