Func键<>>表达℃之间的差异;和Func键<> [英] Difference between Expression<Func<>> and Func<>
问题描述
作为一个例子,为什么大多数LINQ运营商接受表达式来; Func键< TSource>>
及其等价物 Func键< TSource>
?
As an example, why do most LINQ operators accept Expression<Func<TSource>>
and its equivalent Func<TSource>
?
有什么好处/之所以使用,而不是直线上升lambda语法的一般表达
类?
What's the benefit/reason for using the generic Expression
class instead of straight up lambda syntax?
推荐答案
使用表达式来; T>
你明确地创建一个< A HREF =http://msdn.microsoft.com/en-us/library/bb397951.aspx> 表达式树的 - 这意味着你可以处理,使得代码了查询,好像它是数据
Using Expression<T>
you are explicitly creating an expression tree - this means that you can deal with the code that makes up the query as if it were data.
这样做的原因是,LINQ提供(如LINQ到SQL例如)检查查询本身来确定的最佳方式C#的表达式转换成T-SQL查询。由于表达式树可以让你看看代码为数据提供者能够做到这一点。
The reason for this is that LINQ providers (like LINQ to SQL for example) inspect the query itself to determine the best way to translate the C# expressions into a T-SQL query. Since an expression tree lets you look at the code as data the provider is able to do this.
这篇关于Func键<>>表达℃之间的差异;和Func键<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!