Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I am exactly unable to understand what is the use of expression tree.why should we go for expression tree.we can directly execute lambda expression.why should we assign lambda expression to the expression tree to execte.
Ex

XML
Lamda expression
Func<int, int, int> function = (a,b) = > a + b;

This delegate is compiled into executable code in your application
Console.WriteLine("deleg2(4) = {0}", function (4,5))


XML
Expression<Func<int, int, int>> expression = (a,b) => a + b;
Func<int, int, int> expression = expr.Compile();
Console.WriteLine("deleg2(4) = {0}", expression (4,5));
Posted
Updated 14-Oct-10 0:07am
v2

A LINQ to SQL query is not executed inside your C# program. Instead, it is translated into SQL query, sent across a wire, and executed on a database server.
In other words, the following code is never actually executed inside your program:
CSS
var query = from c in db.Customers
            where c.City == "Nantes"
            select new { c.City, c.CompanyName };

It is first translated into the following SQL statement and then executed on a server:
CSS
SELECT [t0].[City], [t0].[CompanyName]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[City] = @p0

It is obviously going to be much easier to translate a data structure such as an expression tree into SQL than it is to translate raw IL or executable code into SQL.

Expression trees are also used in the dynamic language runtime (DLR) to provide interoperability between dynamic languages and the .NET Framework and to enable compiler writers to emit expression trees instead of Microsoft intermediate language (MSIL).

For better understanding refer following link:
Expression Tree[^]
 
Share this answer
 
Hi,

I don't really understand your question but I have some idea what you try to say: why use Func class when you can use lambda expressions straight a way?

Easy answer - for example you want to use some lambda expression few times in code so you will need write it multiply times. If you use Func or Action class you just call this class instance and done. Second reason is when you want to pass some lambda expression to the method - it is also done by delegates like Action or Func.

Best Regards
PZ
 
Share this answer
 
no,what exactly i am asking is advantage of expression tree over lambda expression.

XML
Func<int, int> b = Inc;
Lamda expression
Func<int, int, int> function = (a,b) => a + b;

C++
Console.WriteLine("deleg2(4) = {0}", function (4,5))



XML
Expression<Func<int, int, int>> expression = (a,b) => a + b;
Func<int, int, int> expression = expr.Compile();
Console.WriteLine("deleg2(4) = {0}", expression (4,5));


Both are giveing same result.what is the advantage of expression tree.
 
Share this answer
 
Comments
[no name] 17-Sep-14 6:57am    
This is not a solution. Use the "Improve question" widget to improve your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900