深入學習 C# 表達式樹的最佳資源是什麼? (What is the best resource for learning C# expression trees in depth?)


問題描述

深入學習 C# 表達式樹的最佳資源是什麼? (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.


參考解法

方法 1:

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;
    }
    ...

方法 2:

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"

方法 3:

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.

(by Charlie FlowersTim JarvisMarc Gravellleppie)

參考文件

  1. What is the best resource for learning C# expression trees in depth? (CC BY-SA 3.0/4.0)

#lambda #expression-trees #c#-3.0 #C#






相關問題

Lambda 表達式中的 SQL WHERE 等價物是什麼? (What's the equivalence of an SQL WHERE in Lambda expressions?)

如何將 lambda 傳遞給 Razor 輔助方法? (How to pass in a lambda to a Razor helper method?)

lỗi biểu thức lambda: biểu thức phải là giá trị có thể sửa đổi (lambda expression error: expression must be a modifiable lvalue)

如何在 PySpark 中有效地按值排序? (How to sort by value efficiently in PySpark?)

將列表列表減少為字典,以子列表大小為鍵,出現次數為值 (Reduce list of list to dictionary with sublist size as keys and number of occurances as value)

匿名類作為泛型參數 (anonymous class as generic parameter)

如何為 lambda 中的運算符賦予不定性? (How to give infixities to operators in lambda?)

如何發出委託或 lambda 表達式 (Howto emit a delegate or lambda expression)

深入學習 C# 表達式樹的最佳資源是什麼? (What is the best resource for learning C# expression trees in depth?)

根據最高日期從 IGrouping 中獲取項目 (Get an item from a IGrouping based on the highest date)

如何在 C# 中使用“param”來獲取這個示例? (How can I get this example using "param" in C# to work?)

如何使用 C# 中的 Lambda 表達式僅返回沒有來自另一個表的行的行 (How to return only rows where no rows from another table using Lambda Expressions in C#)







留言討論