我理解lambdas和Func和Action委托。但表情 我头疼不已。
在什么情况下,你会使用表达式<Func<T>>而不是普通的旧Func<T>?
我理解lambdas和Func和Action委托。但表情 我头疼不已。
在什么情况下,你会使用表达式<Func<T>>而不是普通的旧Func<T>?
当前回答
当您希望将lambda表达式视为表达式树并查看其内部而不是执行它们时。例如,LINQ to SQL获取表达式并将其转换为等效的SQL语句并将其提交给服务器(而不是执行lambda)。
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; }
将有效地编译为一个IL方法,该方法得不到任何结果并返回10。
Expression<Func<int>> myExpression = () => 10;
将被转换为描述不获取参数并返回值10的表达式的数据结构:
大图
虽然它们在编译时看起来是一样的,但编译器生成的内容完全不同。
其他回答
当使用LINQ-to-SQL时,将Func<>s传递给Where()或Count()是不好的。真正的坏。如果你使用Func<>,那么它调用IEnumerable LINQ的东西,而不是IQueryable,这意味着整个表被拉进来,然后过滤。表达式<Func<>>明显更快,因为它在SQL服务器上执行过滤-特别是如果您正在查询位于另一个服务器上的数据库。
我想补充一些关于Func<T>和Expression<Func<T>>的区别:
Func<T> is just a normal old-school MulticastDelegate; Expression<Func<T>> 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<T>; 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<T>; the extension methods for IQueryable operate with Expression<Func<T>>.
有一篇文章用代码示例描述了细节: LINQ: Func<T> vs. Expression<Func<T>>。
希望对大家有所帮助。
在Krzysztof Cwalina的书(框架设计指南:可重用的。net库的约定、习惯用法和模式)中有一个更哲学的解释;
编辑非图像版本:
大多数情况下,如果你只需要运行一些代码,你就需要Func或Action。当代码在运行之前需要分析、序列化或优化时,就需要使用Expression。表达式是用来思考代码的,Func/Action是用来运行代码的。
当您希望将lambda表达式视为表达式树并查看其内部而不是执行它们时。例如,LINQ to SQL获取表达式并将其转换为等效的SQL语句并将其提交给服务器(而不是执行lambda)。
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; }
将有效地编译为一个IL方法,该方法得不到任何结果并返回10。
Expression<Func<int>> myExpression = () => 10;
将被转换为描述不获取参数并返回值10的表达式的数据结构:
大图
虽然它们在编译时看起来是一样的,但编译器生成的内容完全不同。
很高兴知道你可以使用Func<TEntity, bool>与AsQueryable()扩展方法,如Expression<Func<TEntity, bool>>。
Func<App, bool> filter = x => x.Alias.Contains("gan");
var query = dbSet.Where(filter).AsQueryable();
在使用Count()或ToList()等执行方法之前,查询不会被执行。