Expression<Func<T, TResult>> represents a strongly-typed expression tree of a lambda or function. Expression trees represent expressions like
a < b stored in a tree structure. So what are they used for? Well, that's open to your imagination honestly. A common scenario is if you want to modify a code expression based on some currently unknown or later business logic. Since an expression tree is a data representation of a code expression you're free to modify it before calling
Compile() and executing it. This is essential for LINQ-to-SQL which transforms expressions like
from a in db.table select a into an expression tree which is then transformed into the necessary SQL for the actual DB call. Expression trees are also heavily used in the Dynamic Language Run-time.
As far as the specific example in your other post:
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
Context is most likely a
Set<T>() returns a
DbSet<T> of all entities of the type given to
Where is called to filter the result based on the predicate expression which, instead of just being a lambda, is stored as an expression tree - I'm guessing because of some other logic elsewhere in the application otherwise this seems a little over-the-top.
EDIT: Forgot to add how to use them. In LINQ you treat it like any other function. Outside of LINQ you call
Compile() to create the
Func delegate. To create them is as simple as assigning a lambda in simple cases. For example,
Expression<Func<int,int>> incrementTree = a => a + 1;. In more complex cases, you may need to explicitly use the expression tree API which you can find links to in the MSDN guide below.
Expression Tree Basics
Expression Trees - MSDN