locked
Expression Trees ? RRS feed

  • Question

  • Hi,
    I was wondering if someone could list out the advantages for using expression trees. I got a grasp on how to use but I can't figure out why I would want to use them.
     
    Sunday, August 24, 2008 11:52 AM

Answers

  • Expression trees were specifically designed to support Linq.  It gives it its "delayed execution" feature.  The Linq query statement builds the tree but nothing happens with the database.  Not until the code starts using the "var" and starts reading results from the query.  It then starts executing the expressions in the tree.  Pretty nice but rarely useful in your own code.  You'd normally write statements that you want to execute right away.  Perhaps it is something you might want to pursue when you are a class library designer and need to provide a class that does something expensive that the class user might want to avoid at her discretion.
    Hans Passant.
    • Marked as answer by jack 321 Wednesday, August 27, 2008 6:51 AM
    Sunday, August 24, 2008 2:29 PM
  •  This link will lead you to a real life example of how the ExpressionTree Visualizer is used in Linq. You should download the Visualizer to a specified location (provided you are interested in Linq) and after that you will be able to use this feature which appears to be rather helpful. Linq is a powerful set of methods that allows you to compress your source code perhaps by a factor of 10 sometimes but it is also fairly counterintuitive.

    Another problem with Linq is that you don't really see the query that is supposed to be generated by the compiler. It is not transparent to you.

    This is a quote from one of the programs that unzips under that download:

    // The Expression Tree Visualizer can be used in a debug environment to visualize expressions. To use
    // the visualizer the dll generated by the ExpressionTreeVisualizer project must be copied to
    // \My Documents\Visual Studio 2008\Visualizers. This console program provides the ability to
    // host the visualizer outside of Visual Studio without copying the DLL.


    AlexB
    Sunday, August 24, 2008 2:45 PM

All replies

  • Expression trees were specifically designed to support Linq.  It gives it its "delayed execution" feature.  The Linq query statement builds the tree but nothing happens with the database.  Not until the code starts using the "var" and starts reading results from the query.  It then starts executing the expressions in the tree.  Pretty nice but rarely useful in your own code.  You'd normally write statements that you want to execute right away.  Perhaps it is something you might want to pursue when you are a class library designer and need to provide a class that does something expensive that the class user might want to avoid at her discretion.
    Hans Passant.
    • Marked as answer by jack 321 Wednesday, August 27, 2008 6:51 AM
    Sunday, August 24, 2008 2:29 PM
  •  This link will lead you to a real life example of how the ExpressionTree Visualizer is used in Linq. You should download the Visualizer to a specified location (provided you are interested in Linq) and after that you will be able to use this feature which appears to be rather helpful. Linq is a powerful set of methods that allows you to compress your source code perhaps by a factor of 10 sometimes but it is also fairly counterintuitive.

    Another problem with Linq is that you don't really see the query that is supposed to be generated by the compiler. It is not transparent to you.

    This is a quote from one of the programs that unzips under that download:

    // The Expression Tree Visualizer can be used in a debug environment to visualize expressions. To use
    // the visualizer the dll generated by the ExpressionTreeVisualizer project must be copied to
    // \My Documents\Visual Studio 2008\Visualizers. This console program provides the ability to
    // host the visualizer outside of Visual Studio without copying the DLL.


    AlexB
    Sunday, August 24, 2008 2:45 PM
  • Got it ! Thanks !

    Monday, August 25, 2008 5:29 AM
  •  Your "thank you" may be a bit premature. I am running into some problems installing the demo. Please take a look. If you have a better luck, please share your experience.
    AlexB
    Monday, August 25, 2008 1:08 PM