Why would you use Expression> rather than Func?

后端 未结 10 2014
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 00:32

I understand lambdas and the Func and Action delegates. But expressions stump me.

In what circumstances would you use an Expression

10条回答
  •  悲&欢浪女
    2020-11-22 01:03

    I'd like to add some notes about the differences between Func and Expression>:

    • Func is just a normal old-school MulticastDelegate;
    • Expression> is a representation of lambda expression in form of expression tree;
    • expression tree can be constructed through lambda expression syntax or through the API syntax;
    • expression tree can be compiled to a delegate Func;
    • the inverse conversion is theoretically possible, but it's a kind of decompiling, there is no builtin functionality for that as it's not a straightforward process;
    • expression tree can be observed/translated/modified through the ExpressionVisitor;
    • the extension methods for IEnumerable operate with Func;
    • the extension methods for IQueryable operate with Expression>.

    There's an article which describes the details with code samples:
    LINQ: Func vs. Expression>.

    Hope it will be helpful.

提交回复
热议问题