views:

1113

answers:

3

When I first typed this question, I did so in order to find the duplicate questions, feeling sure that someone must have already asked this question. My plan was to follow those dupe links instead of posting this question. But this question has not been asked before as far as I can see ... it did not turn up in the "Related Questions" list.

What are some of the best resources you've found (articles, books, blog posts, etc.) for gaining an in-depth understanding of Expression Trees in C#? I keep getting surprised by their capabilities, and now I'm at the point where I'm saying, "OK, enough surprise. I want to stop right now and get a PhD in these things." I'm looking for material that systematically, methodically covers the capabilities and then walks through detailed examples of what you can do with them.

Note: I'm not talking about lambda expressions. I'm talking about Expression< T > and all the things that go with it and arise from it.

Thanks.

+6  A: 

Chapter 11 (Inside Expression Trees) and chapter 12 (Extending Linq) of Programming Microsoft Linq (ISBN 13: 978-0-7356-2400-9 or ISBN 10: 0-7356-2400-3) has been invaluable for me. I haven't read Jons book, but he is a quality guy and explains things well, so I assume that his coverage would also be good.

Another great resource is Bart De Smet's blog

Also, keep your eye on MSDN, the sample code for building a Simple Linq to Database (by Pedram Rezaei) is about to get about 40 pages of Doco explaining it.

EDIT: (I might just keep adding resource links here over time, as it's a useful placeholder for this info.)

A really, really useful resource for Expression Tree's in fact I would regard it as a must have is the Expression Tree Visualiser debugging tool.

You should also learn as much as you can about Expression Tree Visitors, there is a pretty good base class inplementation here.

Here is some sample code derived from that Visitor class to do some debugging (I based this on some sample code in the book I mentioned) the prependIndent method call is just an extension method on a string to put a "--" at each indent level.

  internal class DebugDisplayTree : ExpressionVisitor
  {
    private int indentLevel = 0;

    protected override System.Linq.Expressions.Expression Visit(Expression exp)
    {
      if (exp != null)
      {
        Trace.WriteLine(string.Format("{0} : {1} ", exp.NodeType, exp.GetType().ToString()).PrependIndent(indentLevel));
      }
      indentLevel++;
      Expression result = base.Visit(exp);
      indentLevel--;
      return result;
    }
    ...
Tim Jarvis
From what I can see on Amazon, this book is looking very good. Thanks for sharing.
Charlie Flowers
Re the visualizer - you can do it in one line, too: http://marcgravell.blogspot.com/2009/03/visualizing-expressions-in-propertygrid.html
Marc Gravell
+5  A: 

I don't claim them to be comprehensive, but I have a number of Expression posts on my blog. If you are UK based, I will also be presenting a session on Expression at DDD South West in May (and last night, but too late ;-p). I could post the slide deck and some of the links from related articles, if you want... unfortunately, a pptx intended to be spoken rarely makes sensible standalone reading.

Some other reading (not from the blog):

(and a whole load of posts here and on microsoft.public.dotnet.languages.csharp - try searching for: +expression -regex -"regular expression"

Marc Gravell
+1 - Marc is more "up" with expression trees than anyone else I know.
Jon Skeet
Excellent. I will check these things out. Thank you.
Charlie Flowers
For info, I've also posted some thoughts to InfoQ: http://www.infoq.com/articles/expression-compiler
Marc Gravell
Invaluable. Thanks.
Slavo
+1  A: 

Learn Scheme. Expressions use the same principles as lambda calculus, and hence will give you some better insight.

Alternatively, you can look at the DLR, which is a similar but much less elegant.

leppie