What is the best way to study C# expression trees thoroughly?

c# c#-3.0 expression-trees lambda


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.


3/25/2009 11:43:30 PM

Accepted Answer

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.

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));
      Expression result = base.Visit(exp);
      return result;
7/15/2017 6:11:50 PM

Expert Answer

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"

9/25/2009 5:41:16 PM

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