Tree of Expressions c# expression-trees


My understanding of expression tree is :

Expression trees are in-memory representation of expression like arithmetic or boolean expression.The expressions are stored into the parsed we can easily transalate into any other language.

Linq to SQL uses expression tree.Normally in LINQ to SQL query the compiler translates it into parsed expression trees.These are passed to Sql Server as T-SQL Statements.The Sql server executes the T-SQL query and sends down the result back.That is why when you execute LINQ to SQL you gets IQueryable<T> not IEnumetrable<T>.Because IQuerybale contains

public IQueryable:IEnumerable

   Type Element {get;}
   Expression Expression {get;}
   IQueryaleProvider Provider {get;}

Questions :

  1. Microsoft uses Expression trees to play with LINQ-to-Sql.What are the different ways can i use expression trees to boost my code.

  2. Apart from LINQ to SQL,Linq to amazon ,who used expression trees in their applications?

  3. Linq to Object return IEnumerable,Linq to SQL return IQueryable ,What does LINQ to XML return?

3/28/2010 5:04:45 PM

Accepted Answer

Expression trees offer a way to inspect some piece of code before it is actually compiled, at runtime.

You can do exactly two things with an expression tree:

  • Compile it into a delegate, using Expression.Compile, or
  • Visit the tree nodes and generate something else from it (i.e. a SQL statement).

Expression trees are cool, but the odds of them actually having any direct usefulness for a typical website or enterprise app are remote. You would only use them when you (a) want to generate a different type of code from C# source at runtime, or (b) want to use Reflection but provide some measure of compile-time safety.

If you are working on reusable libraries - DI frameworks, AOP frameworks, MVC/MVP/MVVM frameworks, ORM frameworks, and so on, or trying to extend one of these frameworks (including Linq-to-XYZ), then you will likely find a use for expression trees (such as the Linq to SQL batch update extension), although it's hard to discuss specific examples because it would depend entirely on the design of such a framework.

If you're not building these sorts of tools then expression trees aren't much more than a curiosity. Great to learn about of course - but don't be disappointed if you don't find too many practical uses for them.

A few examples of libraries that use expression trees:

...That should give you an idea of the kind of projects that make serious use of expression trees. So if you're building your own runtime environment or MVC, you definitely want to learn everything you can. ;)

3/28/2010 5:12:22 PM

Popular Answer

  1. What do you mean by 'boost my code'?
    Expression trees will not (usually) increase performance; they are used to enable additional functionality.
  2. Expressions trees have nothing to do with LINQ. For example, they are used by dependency injection systems to specify types and function efficiently.
  3. LINQ to XML returns IEnumerable<XElement>s.

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow