Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best resource for learning C# expression trees in depth?

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.

like image 851
Charlie Flowers Avatar asked Mar 25 '09 21:03

Charlie Flowers


2 Answers

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));       }       indentLevel++;       Expression result = base.Visit(exp);       indentLevel--;       return result;     }     ... 
like image 179
Tim Jarvis Avatar answered Sep 16 '22 20:09

Tim Jarvis


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):

  • Jason Bock: genetic programming with Expression
  • (me again): generic operators with Expression
  • (and again, on InfoQ) Expression as a Compiler

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

like image 27
Marc Gravell Avatar answered Sep 17 '22 20:09

Marc Gravell