ÎÊÌâÃèÊö
ÎÒÁ˽â lambda ÒÔ¼° Func
ºÍ Action
ίÍÐ.µ«ÊDZí´ïʽÄѵ¹ÎÒ.
I understand lambdas and the Func
and Action
delegates. But expressions
stump me.
ÔÚʲôÇé¿öÏÂÄã»áʹÓà Expression<Func<T>>
¶ø²»ÊÇÆÕͨµÄ¾É Func<T>
?
In what circumstances would you use an Expression<Func<T>>
rather than a plain old Func<T>
?
ÍƼö´ð°¸
µ±ÄúÏ뽫 lambda ±í´ïʽÊÓΪ±í´ïʽÊ÷²¢²é¿´ËüÃÇÄÚ²¿¶ø²»ÊÇÖ´ÐÐËüÃÇʱ.ÀýÈ磬LINQ to SQL »ñÈ¡±í´ïʽ²¢½«Æäת»»ÎªµÈЧµÄ SQL Óï¾ä²¢½«ÆäÌá½»¸ø·þÎñÆ÷(¶ø²»ÊÇÖ´ÐÐ lambda).
When you want to treat lambda expressions as expression trees and look inside them instead of executing them. For example, LINQ to SQL gets the expression and converts it to the equivalent SQL statement and submits it to server (rather than executing the lambda).
´Ó¸ÅÄîÉϽ²£¬Expression<Func<T>>
Óë Func<T>
ÍêÈ«²»Í¬.Func
delegate
£¬Ëü¼¸ºõÊÇÒ»¸öÖ¸Ïò·½·¨µÄÖ¸Õ룬¶ø Expression
Expression.Compile
)»òʹÓÃËüÖ´ÐÐÆäËû²Ù×÷(Èç LINQ to SQL ʾÀý).½« lambdas ÊÓΪÄäÃû·½·¨ºÍ±í´ïʽÊ÷µÄÐÐΪ´¿´âÊDZàÒëʱµÄÊÂÇé.
Conceptually, Expression<Func<T>>
is completely different from Func<T>
. Func<T>
denotes a delegate
which is pretty much a pointer to a method and Expression<Func<T>>
denotes a tree data structure for a lambda expression. This tree structure describes what a lambda expression does rather than doing the actual thing. It basically holds data about the composition of expressions, variables, method calls, ... (for example it holds information such as this lambda is some constant + some parameter). You can use this description to convert it to an actual method (with Expression.Compile
) or do other stuff (like the LINQ to SQL example) with it. The act of treating lambdas as anonymous methods and expression trees is purely a compile time thing.
Func<int> myFunc = () => 10; // similar to: int myAnonMethod() { return 10; }
½«ÓÐЧµØ±àÒë³ÉÒ»¸öʲô¶¼²»µÃµ½²¢·µ»Ø 10 µÄ IL ·½·¨.
will effectively compile to an IL method that gets nothing and returns 10.
Expression<Func<int>> myExpression = () => 10;
½«±»×ª»»ÎªÃèÊöÒ»¸öûÓвÎÊý²¢·µ»ØÖµ10µÄ±í´ïʽµÄÊý¾Ý½á¹¹:
will be converted to a data structure that describes an expression that gets no parameters and returns the value 10:
ËäÈ»ËüÃÇÔÚ±àÒëʱ¿´ÆðÀ´Ïàͬ£¬µ«±àÒëÆ÷Éú³ÉµÄÈ´ÍêÈ«²»Í¬.
While they both look the same at compile time, what the compiler generates is totally different.
Õâƪ¹ØÓÚΪʲôҪʹÓñí´ïʽ<Func<T>>¶ø²»ÊÇ Func T ?µÄÎÄÕ¾ͽéÉܵ½ÕâÁË£¬Ï£ÍûÎÒÃÇÍƼöµÄ´ð°¸¶Ô´ó¼ÒÓÐËù°ïÖú£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö¸ú°æÍø£¡