What are Expression Trees, how do you use them, and why would you use them?

Go To StackoverFlow.com


I just came across the concept of expression trees which I have heard multiple times. I just want to understand what is meant by an expression tree and its purpose.

I would love it if someone could also direct me to simple explanations and samples of use.

2012-04-04 06:28
by Donny
possible duplicate of Why would you use Expression<Func<T>> rather than Func<T>?nawfal 2014-07-07 06:52
Answered with few examples ..http://stackoverflow.com/questions/403088/practical-use-of-expression-trees/20470060#2047006 - Moumit 2016-04-25 11:29


An Expression Tree is a data structure that contains Expressions, which is basically code. So it is a tree structure that represents a calculation you may make in code. These pieces of code can then be executed by "running" the expression tree over a set of data.

A great thing about expression trees is that you can build them up in code; that is, you build executable code (or a sequence of steps) in code. You can also modify the code before you execute it by replacing expressions by other expressions.

An Expression is then a function delegate, such as (int x => return x * x).

See also http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx

2012-04-04 06:31
by Roy Dictus
This isn't really true. As pointed out in the linked article, expression trees are mainly used to represent calculations that will be sent across the wire for execution elsewhere. For that reason they do not contain code; rather they are instead of code - david.pfx 2016-06-29 05:53
Expression Trees can be used for any calculation that you want to perform, locally or remotely. You could easily write a parser that would convert a typed expression into an Expression Tree. Also I never said that Expression Trees contain code - Roy Dictus 2016-07-13 14:10
if "tree structure with pieces of code in it" is not intended to mean they "contain code" then you should edit your answer to make that clear. You should make it clear that Expressions do not contain any IL code whatsoever, and rely on being translated further into something executable - david.pfx 2016-07-15 00:23
@david.pfx I would expect readers with the intelligence of software developers to understand that from my answer - Roy Dictus 2016-07-19 09:44
@RoyDictus It is not obvious without reading between the lines. Assuming intelligence is often a bad idea. Please be explicit - Gusdor 2016-07-29 11:23
I would note also, someone who is looking for an answer to this question most likely has no knowledge of the answer and there for the "intelligence" or knowledge of the user should be assumed as 0 (on the subject at hand - Steve Byrne 2016-09-08 17:05
Not sure if this still answers the question? As engineers we try to choose the best ( = meets requirements at least expense) solution. In what class of problem is an expression tree the solution of choice - William T. Mallard 2016-12-15 23:01